目前需要用到jax-ws,是以在進行學習,目前給出一個jax-ws的應用流程:
1.建立接口HelloWorldService接口及方法
/**
* @(#) IHelloService.java
*/
package com.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/**
* @version 1.0
* @Function 類功能說明:建立服務端接口
*/
@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface IHelloWorldService {
@WebMethod
public String sayHelloWorld(@WebParam(name= "name")String name);
}
2.建立接口的實作類,并為接口注入@WebService
/**
* @(#) HelloWorldImpl.java
*/
package com.webservice;
import javax.jws.WebService;
/**
* @version 1.0
* @Function 類功能說明:實作接口,并注入webservice注解
*/
@WebService(endpointInterface = "com.webservice.IHelloWorldService")
public class HelloWorldServiceImpl implements IHelloWorldService{
public String sayHelloWorld(String name) {
return "Hello World : "+name;
}
}
3.編寫釋出類,通過Endpoint将接口釋出出去
/**
* @(#) WsPublisher.java
*/
package com.webservice;
import javax.xml.ws.Endpoint;
/**
* @version 1.0
* @Function 類功能說明:釋出服務端的接口
*/
public class WsPublisher {
public static void main(String[] args) {
System.out.println("開始釋出。。。");
Endpoint.publish("http://127.0.0.1:8012/hello_world", new HelloWorldServiceImpl());
System.out.println("釋出成功。。。");
}
}
運作WsPublisher後,有以下的效果:
開始釋出。。。
2013-8-31 9:24:30 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
資訊: Dynamically creating request wrapper Class com.webservice.jaxws.SayHelloWorld
2013-8-31 9:24:30 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
資訊: Dynamically creating response wrapper bean Class com.webservice.jaxws.SayHelloWorldResponse
釋出成功。。。
這時候就可打開浏覽器檢視http://127.0.0.1:8012/hello_world?wsdl 有如下效果說明釋出成功了
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.com/" name="HelloWorldServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.com/" schemaLocation="http://127.0.0.1:8012/hello_world?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHelloWorld">
<part name="parameters" element="tns:sayHelloWorld"/>
</message>
<message name="sayHelloWorldResponse">
<part name="parameters" element="tns:sayHelloWorldResponse"/>
</message>
<portType name="IHelloWorldService">
<operation name="sayHelloWorld">
<input message="tns:sayHelloWorld"/>
<output message="tns:sayHelloWorldResponse"/>
</operation>
</portType>
<binding name="HelloWorldServiceImplPortBinding" type="tns:IHelloWorldService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHelloWorld">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWorldServiceImplService">
<port name="HelloWorldServiceImplPort" binding="tns:HelloWorldServiceImplPortBinding">
<soap:address location="http://127.0.0.1:8012/hello_world"/>
</port>
</service>
</definitions>
4.建立相關的檔案夾,在cmd指令行下,使用wsgen指令,生成相應的wsdl檔案和異常處理的相關類 wsgen -cp .;bin/ -r ws/wsdl -s ws/src -d ws/bin -wsdl com.webservice.HelloWorldServiceImpl (沒辦法上傳圖檔确實比較坑)
5.建立相關的檔案夾,使用wsimport指令生成用戶端代碼
wsimport -s client/src -d client/bin -p com.webservice.client http://localhost:8012/hello_world?wsdl
生成相應的接口:
com.webservice.client.HelloWorldServiceImplService
com.webservice.client.IHelloWorldService
com.webservice.client.ObjectFactory
com.webservice.client.package-info
com.webservice.client.SayHelloWorld
com.webservice.client.SayHelloWorldResponse
6.最後就可以寫用戶端代碼,進行調用了。
/**
* @(#) HelloWorldClient.java
*/
package com.webservice.client.test;
import com.webservice.client.HelloWorldServiceImplService;
import com.webservice.client.IHelloWorldService;
/**
* @version 1.0
* @Function 類功能說明:用戶端調用
*/
public class HelloWorldClient {
public static void main(String[] args) {
HelloWorldServiceImplService service = new HelloWorldServiceImplService();
IHelloWorldService worldService = service.getHelloWorldServiceImplPort();
System.out.println(worldService.sayHelloWorld("hahaha"));
}
}
運作用戶端輸出結果:
Hello World : hahaha
到這裡整個流程也就跑完了,現在是剛開始學習webservice的jax-ws;以後還需要繼續深入學習,内容會有一定的更新