基于JAX-WS的Web Service服务端/客户端

来源:互联网 发布:mac版itunes铃声 编辑:程序博客网 时间:2024/05/20 05:57

JAX-WS简介:

JAX_RPC(Java API for XML-Based RPC)允许Java应用程序可以通过已知的描述信息调用一个基于Java的Web服务,描述信息与Web服务的WSDL描述相一致

JAX-RPC2.0更名为JAX-WS2.0(java API for XML-Based Web Services)

JAX-WS中,一个远程调用可以转换为一个基于XML的协议,如SOAP。开发者在使用JAX-WS的过程中,不需要编写任何生成、处理SOAP消息的代码,JAX-WS在运行时自动将API的调用转换为相应的SOAP消息

在服务器端,用户只需要通过Java语言定义远程调用所需实现的接口,并提供相应实现,通过调用JAX-WS的服务发布接口即可将其发布为WebService接口

在客户端,用户可以通过JAX-WS的API创建一个代理来实现对于远程服务器端的调用

 

JAX-WS服务端:

JAX-WS服务端采用注释描述WebService,不再依赖WebService描述文件

使用JDK1.6_45(JDK1.5中不包含所需类)

[java] view plain copy
  1. package com.sean.server;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7. @WebService  
  8. public interface Plus {  
  9.       
  10.     @WebMethod  
  11.     public int plus(@WebParam(name="x"int x,@WebParam(name="y"int y);  
  12. }  
[java] view plain copy
  1. package com.sean.server;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7. @WebService  
  8. public class PlusImpl implements Plus {  
  9.       
  10.     @WebMethod  
  11.     public int plus(@WebParam(name="x"int x, @WebParam(name="y"int y) {  
  12.         return x + y;  
  13.     }  
  14. }  
[java] view plain copy
  1. package com.sean.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. public class Server {  
  6.     public static void main(String[] args) {  
  7.         PlusImpl plus = new PlusImpl();  
  8.         String addr = "http://127.0.0.1:8888/Plus";  
  9.         Endpoint.publish(addr, plus);  
  10.     }  
  11. }  

程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is   
  3.     JAX-WS RI 2.1.6 in JDK 6. -->  
  4. <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is   
  5.     JAX-WS RI 2.1.6 in JDK 6. -->  
  6. <definitions name="PlusImplService" targetNamespace="http://server.sean.com/"  
  7.     xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  8.     xmlns:tns="http://server.sean.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">  
  9.     <types>  
  10.         <xsd:schema>  
  11.             <xsd:import schemaLocation="http://127.0.0.1:8888/Plus?xsd=1"  
  12.                 namespace="http://server.sean.com/" />  
  13.         </xsd:schema>  
  14.     </types>  
  15.     <message name="plus">  
  16.         <part name="parameters" element="tns:plus" />  
  17.     </message>  
  18.     <message name="plusResponse">  
  19.         <part name="parameters" element="tns:plusResponse" />  
  20.     </message>  
  21.     <portType name="PlusImpl">  
  22.         <operation name="plus">  
  23.             <input message="tns:plus" />  
  24.             <output message="tns:plusResponse" />  
  25.         </operation>  
  26.     </portType>  
  27.     <binding name="PlusImplPortBinding" type="tns:PlusImpl">  
  28.         <soap:binding style="document"  
  29.             transport="http://schemas.xmlsoap.org/soap/http" />  
  30.         <operation name="plus">  
  31.             <soap:operation soapAction="" />  
  32.             <input>  
  33.                 <soap:body use="literal" />  
  34.             </input>  
  35.             <output>  
  36.                 <soap:body use="literal" />  
  37.             </output>  
  38.         </operation>  
  39.     </binding>  
  40.     <service name="PlusImplService">  
  41.         <port name="PlusImplPort" binding="tns:PlusImplPortBinding">  
  42.             <soap:address location="http://127.0.0.1:8888/Plus" />  
  43.         </port>  
  44.     </service>  
  45. </definitions>  

使用 SoapUI5.0.0尝试用上面的WSDL创建WebService服务端报错(org.apache.xmlbeans.XmlException:error:does not close tag.)

使用SoapUI4.5.2则一切正常,只能归咎于不同版本的SoapUI对文件格式校验不同

 

JAX-WS客户端:

[java] view plain copy
  1. package com.sean.client;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import javax.xml.namespace.QName;  
  6. import javax.xml.ws.Service;  
  7.   
  8. import com.sean.server.Plus;  
  9.   
  10. public class Client {  
  11.     public static void main(String[] args) throws Exception {  
  12.         QName serviceName = new QName("http://server.sean.com/""PlusImplService");  
  13.         QName portName = new QName("http://server.sean.com/""PlusImplPort");  
  14.           
  15.         String addr = "http://127.0.0.1:8888/Plus?wsdl";  
  16.         URL url = new URL(addr);  
  17.           
  18.         Service service = Service.create(url, serviceName);  
  19.         Plus plus = service.getPort(portName,Plus.class);  
  20.         //Plus plus = service.getPort(Plus.class);  
  21.           
  22.         int result = plus.plus(12);  
  23.         System.out.println("result:" + result);  
  24.     }  
  25. }  

使用Plus plus = service.getPort(Plus.class)方法时,客户端调用时报错:

[plain] view plain copy
  1. Exception in thread "main" javax.xml.ws.WebServiceException: Undefined port type: {http://server.sean.com/}Plus  

解决方式一:

如客户端示例,将Plus plus = service.getPort(Plus.class)修改为Plus plus = service.getPort(portName,Plus.class)

解决方式二:

修改PlusImpl类的@WebService标记修改为@WebService(endpointInterface="com.sean.server.Plus")

参考:http://stackoverflow.com/questions/13417454/javax-xml-ws-webserviceexception-undefined-port-type-java-struts-soap-wsdl 

文章出处:http://blog.csdn.net/a19881029/article/details/32101373

原创粉丝点击