使用JDK編寫webService
阿新 • • 發佈:2018-12-26
在JDK1.6以及以上版本中,已經加入了原生的webService的支援,通過簡單的編寫就可以實現一個java webService
一、編寫伺服器程式碼
1、定義SEI(ServiceEndpoint Interface 釋出的服務介面)
首先,建立一個普通的java project,命名為ws_server,並在src下建立一個包,命名為com.study.ws_server
之後建立名稱為HelloWS的介面,其中內容如下:
HelloWS.java
package com.study.ws_server; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface HelloWS { @WebMethod public String sayHello(String name); }
2、實現webService伺服器程式碼
建立類HelloWSImpl,並實現HelloWS
HelloWSImpl.java
package com.study.ws_server; import javax.jws.WebService; @WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("com.study.ws_server.HelloWSImpl: " + name); return "Hello " + name; } }
這裡僅僅是接收到的引數簡單處理一下就返回了
3、釋出webService
建立包com.study.ws_server.server,並建立類ServerTest
ServerTest.java
package com.study.ws_server.server; import javax.xml.ws.Endpoint; import com.study.ws_server.HelloWS; import com.study.ws_server.HelloWSImpl; public class ServerTest { public static void main(String[] args) { String address = "http://localhost:8888/ws_server"; HelloWS helloWS = new HelloWSImpl(); Endpoint.publish(address, helloWS); System.out.println("ServerTest publish success!"); } }
編寫完成,執行ServerTest.java,控制檯提示成功後,在瀏覽器輸入http://localhost:8888/ws_server?wsdl,得到的內容如下:
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<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_server.study.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws_server.study.com/" name="HelloWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws_server.study.com/" schemaLocation="http://localhost:8888/ws_server?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="HelloWSImpl">
<operation name="sayHello">
<input wsam:Action="http://ws_server.study.com/HelloWSImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://ws_server.study.com/HelloWSImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloWSImplPortBinding" type="tns:HelloWSImpl">
<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="HelloWSImplService">
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8888/ws_server"/>
</port>
</service>
</definitions>
根據上面的內容可以生成客戶端程式碼
二、編寫客戶端程式碼
1、根據WSDL檔案生成客戶端程式碼
建立java project 命名為ws_client
滑鼠右鍵src,選擇terminal
在彈出的終端中輸入wsimport -keep http://localhost:8888/ws_server?wsdl,並回車
等待提示程式碼生成完畢後重新整理src,可以看到自動生成的程式碼
2、呼叫伺服器webService介面
建立包com.study.ws_client.client,並建立類ClientTest
ClientTest.java
package com.study.ws_client.client;
import com.study.ws_server.HelloWSImpl;
import com.study.ws_server.HelloWSImplService;
public class ClientTest {
public static void main(String[] args) {
HelloWSImplService service = new HelloWSImplService();
HelloWSImpl helloWSImplPort = service.getHelloWSImplPort();
String result = helloWSImplPort.sayHello("Andy");
System.out.println("result content: " + result);
}
}
編寫完畢後,執行ClientTest,控制檯顯示結果為:
result content: Hello Andy