1. 程式人生 > >WebService筆記(第二彈:使用JAX-WS開發WebService)

WebService筆記(第二彈:使用JAX-WS開發WebService)

我們現在先來使用JDK的JAX-WS來開發一個WebService。
JAX-WS規範是一組XML web services的JAVA API。JAX-WS允許開發者可以選擇RPC-oriented或者message-oriented 來實現自己的web services。
在 JAX-WS中,一個遠端呼叫可以轉換為一個基於XML的協議例如SOAP。在使用JAX-WS過程中,開發者不需要編寫任何生成和處理SOAP訊息的程式碼。JAX-WS的執行時實現會將這些API的呼叫轉換成為對於SOAP訊息。
在伺服器端,使用者只需要通過Java語言定義遠端呼叫所需要實現的介面SEI (service endpoint interface),並提供相關的實現,通過呼叫JAX-WS的服務釋出介面就可以將其釋出為WebService介面。
在客戶端,使用者可以通過JAX-WS的API建立一個代理(用本地物件來替代遠端的服務)來實現對於遠端伺服器端的呼叫。
通過web service所提供的互操作環境,我們可以用JAX-WS輕鬆實現JAVA平臺與其他程式設計環境(.net等)的互操作。

定義服務端WebService

我們使用JDK的JAX-WS定義WebService,需要使用@WebService註解在WebService類的介面上進行標記。我們先來定義一個叫做MyWS的WebService介面。

package com.hello.ws;

import javax.jws.WebService;

@WebService
public interface MyWS {

    public String sayHello(String name);
}

對應的實現類如下:

package com.hello.ws;

import javax.jws.WebService;

@WebService
public class MyWSImpl implements MyWS { @Override public String sayHello(String name) { return "I am web service, hello " + name; } }

釋出服務端WebService

我們使用Endpoint類來進行釋出。

package com.hello.ws;

import javax.xml.ws.Endpoint;

public class MyEndPoint {

    public static void
main(String[] args) { String address = "http://localhost:8089/ws/MyWS"; Endpoint.publish(address, new MyWSImpl()); } }
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.hello.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.hello.com/" name="MyWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.hello.com/" schemaLocation="http://localhost:8089/ws/MyWS?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="MyWSImpl">
<operation name="sayHello">
<input wsam:Action="http://ws.hello.com/MyWSImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://ws.hello.com/MyWSImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="MyWSImplPortBinding" type="tns:MyWSImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyWSImplService">
<port name="MyWSImplPort" binding="tns:MyWSImplPortBinding">
<soap:address location="http://localhost:8089/ws/MyWS"/>
</port>
</service>
</definitions>

生成WebService客戶端程式碼

我們要使用jdk的wsimort.exe工具生成客戶端程式碼,wsimort.exe工具位於Jdk的bin目錄下。
我們新建一個簡單的WebService客戶端工程,開啟cmd命令列視窗,cd到新建出來的客戶端工程的src目錄,執行命令:“wsimport -keep http://localhost:8089/ws/MyWS?wsdl”生成客戶端程式碼。我們在src目錄下可以看到生成出來的客戶端程式碼。

在客戶端呼叫WebService對外提供的方法

wsimport工具幫我們生成了好幾個.java和.class檔案,但我們只需要關心MyWSImplService類和MyWSImpl介面的使用即可。

package com.hello.ws;

public class WSClient {

    public static void main(String[] args) {
        // 建立一個用於產生MyWSImpl例項的工廠,MyWSImplService類是wsimport工具生成的
        MyWSImplService factory = new MyWSImplService();
        // 通過工廠生成一個MyWSImpl例項,MyWSImpl是wsimport工具生成的
        MyWSImpl impl = factory.getMyWSImplPort();
        // 呼叫WebService的方法
        String result = impl.sayHello("wsclient");
        System.out.println(result);
    }

}

執行一把這段程式碼,可以看到控制檯輸出:
I am web service, hello wsclient
這說明,wsimport工具生成的客戶端程式碼已經成功呼叫到了服務端WebService中的方法。

以上,我們就實現了使用JAX-WS開發WebService。