1、使用Myeclipse 8.5开发基于JAX-WS的Web service实例本文为Web service 开发入门篇,主要介绍在Myeclipse 8.5环境下开发Web service的服务程序和客户端程序的基本流程。在Weblogic 10.3.4 中部署Web service服务。开发环境如下:JAVA IDE: Myeclipse 8.5Web server: Weblogic 10.3.4开发Web service服务程序,需要了解以下相关内容, WSDL, SOAP, XML。这些是组成Web service 的基础。在Myeclipse8.5下开发Web service程序,目
2、前系统支持的开发框架有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。Fi
3、nish完成项目新建。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 S
4、ervices - 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定义文件,WebRootWEB-INFwsdlSayHelloService.wsdlWebRootWEB-INFwsdlSayHelloSe
5、rvice_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,如果显示正确,则服务发布成功。3. 在创建Web service 项目的过程中,如果加入了 JAX-WS 2.1 Runtime / API 库,在部署W
6、eb 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!/WE
7、B-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/li
8、b/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_RE
9、FERENCE_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这两个包 ,再重
10、新部署就可以了。4.如果在Tomcat 6.0上部署,则需要加上JAX-WS API /Runtime两个库,否则会报错,严重: Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSServletContextListenerjava.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListenerat org.apache.catalina.loader
11、.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,输入项目名
12、UseWS, Finish 结束新建。2. 新建Web service client程序, File New - Other - MyEclipse - Web Services - Web Service Client,Next 进入下一步,WSDL File 为Web Service 的WSDL文件E:workspacemyWebSvcWebRootWEB-INFwsdlSayHelloService.wsdlNext进入下一步,Finish 结束新建。3. 新建一个Test类,用来调用 SayHelloService, File - New - Class,Finish 结束类的新建。修
13、改类如下: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
14、 = sayHelloDgt.sayHaha(request);System.out.println(response); 4. 运行项目,发现一个错误,Exception in thread main com.sun.xml.ws.client.ClientTransportException: HTTP transport error: .ConnectException: Connection refused: connectat com.sun.xml.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTr
15、ansport.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(F
16、iber.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 co
17、m.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: .ConnectExc
18、eption: Connection refused: connectat .PlainSocketImpl.socketConnect(Native Method)at .PlainSocketImpl.doConnect(PlainSocketImpl.java:333)at .PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)at .PlainSocketImpl.connect(PlainSocketImpl.java:182)at .Socket.connect(Socket.java:507)at .Socket.c
19、onnect(Socket.java:457)at .NetworkClient.doConnect(NetworkClient.java:157)at .www.http.HttpClient.openServer(HttpClient.java:365)at .www.http.HttpClient.openServer(HttpClient.java:477)at .www.http.HttpClient.(HttpClient.java:214)at .www.http.HttpClient.New(HttpClient.java:287)at .www.http.HttpClient
20、.New(HttpClient.java:299)at .www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)at .www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:771)at .www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:669)at .www.protocol.http.HttpURLConnect
21、ion.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, 修改为 另外,如果不在同一台机器上测试, 这里的localhost应该改为相应的IP地址。修改后测试,显示成功。Haha, World, O.K. !