JAVA 快速实现webService (同步逻辑)

来源:互联网 发布:融合网络 英文 编辑:程序博客网 时间:2024/04/20 08:30

今天因为需要调用第三方的接口些fake service, 用于testcase. 该第三方使用的是soap的webservice模式。

首先是创建service。 

[java] view plaincopyprint?
  1. package myws;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.jws.WebMethod;  
  6. import javax.jws.WebParam;  
  7. import javax.jws.WebResult;  
  8. import javax.jws.WebService;  
  9. import javax.xml.bind.annotation.XmlAccessType;  
  10. import javax.xml.bind.annotation.XmlAccessorType;  
  11. import javax.xml.bind.annotation.XmlRootElement;  
  12. import javax.xml.ws.Endpoint;  
  13.   
  14. /** 
  15.  * 
  16.  * @author 帐前卒 
  17.  * 
  18.  */  
  19. @WebService(  
  20.         name="HELLO",  
  21.         targetNamespace="http://chillyc.info/api",   
  22.         serviceName="API",   
  23.         portName="PortName")  
  24. public class WebServiceHolder {  
  25.     @WebMethod  
  26.     @WebResult(name="return")  
  27.     public String hello(@WebParam(name="name")String name) {  
  28.         return "hello" + name;  
  29.     }  
  30.       
  31.     public static void main(String[] args) throws IOException {  
  32.         Endpoint.publish("http://localhost:80/fake/ws"new WebServiceHolder());  
  33.         System.in.read();  
  34.     }  
  35. }  

这里写System.in.read();是希望服务在那里卡死。基本上所有的server都是类似死循环的写法。所以我这里就偷懒使用IO.

这里要注意的是@WebService annotation. 其中 name是指这个portType 叫什么。 targetNameSpace这个在所有的后续调用中名字都是一致的。serviceName就是服务的名称。portName 其实就是提供服务的端口名称(这里对服务本身的调用没有什么关系)。如果没有name, 那么java会默认使用WebServiceHolder 也就类名称作为name.

运行后,在浏览器中打开 

[java] view plaincopyprint?
  1. http://localhost:80/fake/ws?wsdl  
然后就可以看到wsdl文件。

[html] view plaincopyprint?
  1. This XML file does not appear to have any style information associated with it. The document tree is shown below.  
  2. <!-- 
  3.  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
  4. -->  
  5. <!-- 
  6.  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
  7. -->  
  8. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://chillyc.info/api" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://chillyc.info/api" name="API">  
  9. <types>  
  10. <xsd:schema>  
  11. <xsd:import namespace="http://chillyc.info/api" schemaLocation="http://localhost/fake/ws?xsd=1"/>  
  12. </xsd:schema>  
  13. </types>  
  14. <message name="hello">  
  15. <part name="parameters" element="tns:hello"/>  
  16. </message>  
  17. <message name="helloResponse">  
  18. <part name="parameters" element="tns:helloResponse"/>  
  19. </message>  
  20. <message name="getReturnInfo">  
  21. <part name="parameters" element="tns:getReturnInfo"/>  
  22. </message>  
  23. <message name="getReturnInfoResponse">  
  24. <part name="parameters" element="tns:getReturnInfoResponse"/>  
  25. </message>  
  26. <portType name="HELLO">  
  27. <operation name="hello">  
  28. <input message="tns:hello"/>  
  29. <output message="tns:helloResponse"/>  
  30. </operation>  
  31. <operation name="getReturnInfo">  
  32. <input message="tns:getReturnInfo"/>  
  33. <output message="tns:getReturnInfoResponse"/>  
  34. </operation>  
  35. </portType>  
  36. <binding name="PortNameBinding" type="tns:HELLO">  
  37. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>  
  38. <operation name="hello">  
  39. <soap:operation soapAction=""/>  
  40. <input>  
  41. <soap:body use="literal"/>  
  42. </input>  
  43. <output>  
  44. <soap:body use="literal"/>  
  45. </output>  
  46. </operation>  
  47. <operation name="getReturnInfo">  
  48. <soap:operation soapAction=""/>  
  49. <input>  
  50. <soap:body use="literal"/>  
  51. </input>  
  52. <output>  
  53. <soap:body use="literal"/>  
  54. </output>  
  55. </operation>  
  56. </binding>  
  57. <service name="API">  
  58. <port name="PortName" binding="tns:PortNameBinding">  
  59. <soap:address location="http://localhost/fake/ws"/>  
  60. </port>  
  61. </service>  
  62. </definitions>  

大家自己对照刚才的那些name, serviceName等,在wsdl文件中的什么地方。

然后是写一个stub作为调用的接口。

[java] view plaincopyprint?
  1. package myws;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. /** 
  7.  * 
  8.  * @author 帐前卒 
  9.  * 
  10.  */  
  11. @WebService(targetNamespace = "http://chillyc.info/api", name="HELLO")  
  12. public interface WebServiceAPI {  
  13.     String hello(@WebParam(name="name")String name);  
  14.   
  15. }  


这里要注意的是 那个hello函数,必须与webService发布的函数名相一致(要看wdsl文件中的名字。) 另外WebParam中的name也需要和发布函数中的参数名字一致。 这里WebService中传入了两个值。其中name就是刚才WebService中的name. 其实就是wsdl中的portType. 如果这里写错了。就会有Undefined port type:{http://chillyc.info/api}Name. 这个错误。所以要小心。

另外写个可执行的类。
[java] view plaincopyprint?
  1. package myws;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.ws.Service;  
  8.   
  9. /** 
  10.  * 
  11.  * @author 帐前卒 
  12.  * 
  13.  */  
  14. public class Client {  
  15.     public static void main(String[] args) throws MalformedURLException {  
  16.         WebServiceAPI api = Service.create(  
  17.                 new URL("http://localhost:80/fake/ws?wsdl"),  
  18.                 new QName("http://chillyc.info/api""API"))  
  19.                 .getPort(WebServiceAPI.class);  
  20.         System.out.println(api.hello("sss"));  
  21.     }  
  22. }  

这里URL中的就是wsdl文件的地址。 QName传入的就是targetNamespace 和 serviceName. 另外getPort就填入刚才的stub. 然后直接调用stub中的接口就能得到结果。

done.

简单快速,搭建和写client 也就是10分钟搞定。当然这只是起步

这个方法适用于JDK6及以上,其他版本未知。

原创粉丝点击