在Myeclipse8.5下開發Web service程序,目前系統支持的開發框架有3個,JAX-WS,REST(JAX-RS),XFire。其中系統建議不要使用XFire 的框架,可能是要被淘汰了(deprecated)。我們選擇的是JAX-WS框架。這個需要Java EE 5.0的支持。所以後面選擇Web server容器的時候,是要能支持Java EE 5.0的才可以部署成功。
(一) Web service服務端開發
1. 新建一個Web service project, 菜單File -> New -> Web Service Project,
在上面的框中輸入項目名myWebSvc,框架選擇 JAX-WS, J2EE只能選擇Java EE 5.0。
Finish完成項目新建。
2. 新建一個 Java Bean, 就是一個普通的Class, File –> New -> Class,
包名輸入 com.myweb.ws, 類名輸入 SayHello, Finish 結束新建類。
在類中加入一個方法sayHaha如下,
package com.myweb.ws;
public class SayHello
{
public String sayHaha(String request)
{
return "Haha, " + request + ", O.K. !";
}
}
3. 新建一個Web service, 菜單File –> New -> Other -> MyEclipse -> Web Services -> Web Service,
Next 進入下一步,
選擇 Create web service from java class(Bottom-up scenario),Next進入下一步,
Java class 輸入 com.myweb.ws.SayHello, 選中 Generate WSDL in project 選項,
Finish 結束新建。
生成新的服務代理類, com.myweb.ws.SayHelloDelegation
生成WSDL定義文件,WebRoot\WEB-INF\wsdl\SayHelloService.wsdl
WebRoot\WEB-INF\wsdl\SayHelloService_Schema1.xsd,
(二) 部署Web service 服務
1. Window -> Show View -> Servers, 這裡前提是已經安裝完成Weblogic10.3.4,
在 Weblogic 10.x上點右鍵, 選擇Add Deployment 部署myWebSvc,
Finish結束部署。
2. 重啟Weblogic 服務,在浏覽器輸入 http://localhost:7001/myWebSvc/SayHelloPort,如果失敗 在浏覽器輸入
http://localhost:7001/WebRoot/SayHelloPort 如果顯示正確,則服務發布成功。
3. 在創建Web service 項目的過程中,如果加入了 JAX-WS 2.1 Runtime / API 庫,在部署Web service時,會報錯誤,
2011-10-13 17:28:22 [com.sun.xml.ws.policy.jaxws.BuilderHandler] getPolicySubjects
嚴重: [failed to localize] WSP_1014_POLICY_REFERENCE_DOES_NOT_EXIST(zip:D:/bea/weblogic1034/webdomain/servers/AdminServer/tmp/_WL_user/_appsdir_WebServicePro_dir/achpn5/war/WEB-INF/lib/webservices-rt.jar!/WEB-INF/wsdl/wsat.wsdl#Addressing_policy)
2011-10-13 17:28:22 [com.sun.xml.ws.policy.jaxws.PolicyWSDLParserExtension] finished
嚴重: [failed to localize] WSP_1014_POLICY_REFERENCE_DOES_NOT_EXIST(zip:D:/bea/weblogic1034/webdomain/servers/AdminServer/tmp/_WL_user/_appsdir_WebServicePro_dir/achpn5/war/WEB-INF/lib/webservices-rt.jar!/WEB-INF/wsdl/wsat.wsdl#Addressing_policy)
2011-10-13 17:28:22 [com.sun.xml.ws.policy.jaxws.PolicyWSDLParserExtension] finished
嚴重: [failed to localize] WSP_1018_POLICY_EXCEPTION_WHILE_FINISHING_PARSING_WSDL()
com.sun.xml.ws.policy.PolicyException: [failed to localize] WSP_1014_POLICY_REFERENCE_DOES_NOT_EXIST(zip:D:/bea/weblogic1034/webdomain/servers/AdminServer/tmp/_WL_user/_appsdir_WebServicePro_dir/achpn5/war/WEB-INF/lib/webservices-rt.jar!/WEB-INF/wsdl/wsat.wsdl#Addressing_policy)
at com.sun.xml.ws.policy.jaxws.BuilderHandler.getPolicies(BuilderHandler.java:93)
只要刪去JAX-WS這兩個包 ,再重新部署就可以了。
4. 如果在Tomcat 6.0上部署,則需要加上JAX-WS API / Runtime 兩個庫,否則會報錯,
嚴重: Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4078)
(三) Web service 客戶端開發
1. 新建 Java project, File -> New -> Java Project,
輸入項目名 UseWS, Finish 結束新建。
2. 新建Web service client程序, File –> New -> Other -> MyEclipse -> Web Services -> Web Service Client,
Next 進入下一步,
WSDL File 為Web Service 的WSDL文件
E:\workspace\myWebSvc\WebRoot\WEB-INF\wsdl\SayHelloService.wsdl
Next進入下一步,
Finish 結束新建。
3. 新建一個Test類,用來調用 SayHelloService, File -> New -> Class,
Finish 結束類的新建。
修改類如下:
import com.myweb.ws.SayHelloDelegate;
import com.myweb.ws.SayHelloService;
public class Test
{
public static void main(String[] args)
{
String request = "World";
String response = null;
SayHelloService sayHelloSvc = new SayHelloService();
SayHelloDelegate sayHelloDgt = sayHelloSvc.getSayHelloPort();
response = sayHelloDgt.sayHaha(request);
System.out.println(response);
}
}
4. 運行項目,發現一個錯誤,
Exception in thread "main" com.sun.xml.ws.client.ClientTransportException: HTTP transport error: java.net.ConnectException: Connection refused: connect
at com.sun.xml.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:134)
at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:140)
at com.sun.xml.xwss.XWSSClientPipe.process(XWSSClientPipe.java:118)
at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:115)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)
at com.sun.xml.ws.client.Stub.process(Stub.java:248)
at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
at $Proxy34.sayHaha(Unknown Source)
at Test.main(Test.java:15)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
at sun.net.www.http.HttpClient.New(HttpClient.java:287)
at sun.net.www.http.HttpClient.New(HttpClient.java:299)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:771)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:669)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:836)
at com.sun.xml.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:122)
... 14 more
錯誤顯示是Connection refused, 查看了SayHelloService.wsdl文件,發現這裡的配置文件中, 標簽service 的soap:address location 的端口是8080, 而Weblogic中我們用的默認端口是7001,於是修改端口號為7001,
<service name="SayHelloService">
<port binding="tns:SayHelloPortBinding" name="SayHelloPort">
<soap:address location="http://localhost:8080/myWebSvc/SayHelloPort"/>
</port>
</service>
修改為
<service name="SayHelloService">
<port binding="tns:SayHelloPortBinding" name="SayHelloPort">
<soap:address location="http://localhost:7001/myWebSvc/SayHelloPort"/>
</port>
</service>
另外,如果不在同一台機器上測試, 這裡的localhost應該改為相應的IP地址。
修改後測試,顯示成功。
Haha, World, O.K. !