在java ee5之前,要使用java技術開發web service,需要藉助第三方的架構xfire,axis,cxf等,現在藉助最新的java ee5技術,不再需要向項目中引入大量第三方的jar檔案,有jdk 6就夠了。
使用步驟:
1.web service代碼
package com.test;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService(targetNamespace="http://demo.test.org/hello")
@SOAPBinding(style=Style.RPC)
public class TestService {
@WebMethod(operationName="Hello")
public String sayHello(@WebParam(name="name")String msg){
return "Hello :"+msg;
}
}
代碼很簡單,不需要實現什麼介面,也不需要繼承自什麼特殊的超類,與普通的java類相比,最大的區別是大量的使用了注釋技術。注意上面的@SOAPBinding(style=Style.RPC)不能省略,否則會報異常:
嚴重: StandardWrapper.Throwable
com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.test.jaxws.SayHello is not found. Have you run APT to generate them?
嚴重: Servlet /m4 threw load() exception
com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.test.jaxws.SayHello is not found. Have you run APT to generate them?
2.發布服務
和以前的web service開發技術相比較,現在需要用程式碼發布一下自己開發的web serivce,建立一個serlvet,在init代碼中寫發布代碼
package com.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Endpoint;
public class WebServiceLoader extends HttpServlet {
private static final long serialVersionUID = 1L;
public WebServiceLoader() {
super();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
System.out.println("Web Service發布中......");
Endpoint.publish("http://0.0.0.0:8081/Hello", new TestService());
System.out.println("Web Service發布完成");
}
}
注意連接埠號碼問題,上面的範例程式碼中使用的是8081連接埠,如果發布到已經被佔用的連接埠將會失敗(如tomcat本身工作在8080,如果再發布到8080就會失敗。)
3.配置servlet自動載入
開啟web.xml,配置servlet為自動載入
<servlet>
<servlet-name>WebServiceLoader</servlet-name>
<servlet-class>com.test.WebServiceLoader</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
4.訪問wsdl
部署程式,啟動容器,如tomcat等,然後開啟瀏覽器,訪問 http://localhost:8081/Hello?wsdl 可以看到wsdl描述符。
<?xml version="1.0" encoding="UTF-8" ?> - <!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6.
-->
- <!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6.
-->
- < definitions xmlns:soap ="
http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:tns ="
http://demo.test.org/hello " xmlns:xsd ="
http://www.w3.org/2001/XMLSchema " xmlns ="
http://schemas.xmlsoap.org/wsdl/ " targetNamespace ="
http://demo.test.org/hello " name ="
TestServiceService ">
< types />
- < message name ="
Hello ">
< part name ="
name " type ="
xsd:string " />
</ message >
- < message name ="
HelloResponse ">
< part name ="
return " type ="
xsd:string " />
</ message >
- < portType name ="
TestService ">
- < operation name ="
Hello " parameterOrder ="
name ">
< input message ="
tns:Hello " />
< output message ="
tns:HelloResponse " />
</ operation >
</ portType >
- < binding name ="
TestServicePortBinding " type ="
tns:TestService ">
< soap:binding transport ="
http://schemas.xmlsoap.org/soap/http " style ="
rpc " />
- < operation name ="
Hello ">
< soap:operation soapAction ="
" />
- < input >
< soap:body use ="
literal " namespace ="
http://demo.test.org/hello " />
</ input >
- < output >
< soap:body use ="
literal " namespace ="
http://demo.test.org/hello " />
</ output >
</ operation >
</ binding >
- < service name ="
TestServiceService ">
- < port name ="
TestServicePort " binding ="
tns:TestServicePortBinding ">
< soap:address location ="
http://localhost:8081/Hello " />
</ port >
</ service >
</ definitions >