使用Myeclipse 8.5开发基于JAX-WS的Web service实例
本文为Web service 开发入门篇,主要介绍在Myeclipse 8.5环境下开发Web service的服务程序和客户端程序的基本流程。
在Weblogic 10.3.4 中部署Web service服务。
开发环境如下:
JAVA IDE: Myeclipse 8.5
Web server: Weblogic 10.3.4
开发Web service服务程序,需要了解以下相关内容, WSDL, SOAP, XML。这些是组成Web service 的基础。
在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,如果显示正确,则服务发布成功。
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,
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. !
(全文完)