webservice WSDL java例子

http://blog.csdn.net/yaerfeng/article/details/8352307

 

WEBSERVICE快速入门的示例:

首先定义接口:

 

[java] view plain copy  print?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /** 
  6.  * @className:IMyservice.java 
  7.  * @Desc:定义:SEI service endpoint interface 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午12:57:18 
  10.  */  
  11. //JAX-WS注解,表示java api xml for webservice。JDK自带API的XML格式的webservice  
  12. @WebService  
  13. public interface IMyservice {  
  14.       
  15.     int add(int a, int b);  
  16.   
  17.     int minus(int a, int b);  
  18. }  


其次编写实现类:

 

 

[java] view plain copy  print?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /**  
  6.  * @className:MyServiceImpl.java 
  7.  * @Desc:定义:SIB service implemention bean 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午01:01:22 
  10.  */  

[java] view plain copy  print?

  1. //endpointInterface指定接入点接口:接口必须存在  
  2. @WebService(endpointInterface="com.whaty.platform.ws.server.IMyservice")  
  3. public class MyServiceImpl implements IMyservice {  
  4.   
  5.     public int add(int a, int b) {  
  6.         System.out.println("a+b="+(a+b));  
  7.         return a+b;  
  8.     }  
  9.   
  10.     public int minus(int a, int b) {  
  11.         System.out.println("a-b="+(a-b));  
  12.         return a-b;  
  13.     }  
  14.   
  15. }  


最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:

 

 

[java] view plain copy  print?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. /** 
  6.  * @className:MyServer.java 
  7.  * @Desc:发布服务 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午01:02:39 
  10.  */  
  11. public class MyServer {  
  12.     public static void main(String[] args) {  
  13.         //访问方式:http://localhost:7777/tudou?wsdl  
  14.         String address="http://localhost:7777/tudou";  
  15.         Endpoint.publish(address, new MyServiceImpl());  
  16.     }  
  17. }  

 

 

浏览器地址栏输入:访问webservice看看是否发布成功【地址后面加上"?wsdl"】:

http://localhost:7777/tudou?wsdl

 

浏览器显示如下:

 

[html] view plain copy  print?

  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://server.ws.platform.whaty.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.platform.whaty.com/" name="MyServiceImplService">  
  9. <types>  
  10. <xsd:schema>  
  11. <xsd:import namespace="http://server.ws.platform.whaty.com/" schemaLocation="http://localhost:7777/tudou?xsd=1"/>  
  12. </xsd:schema>  
  13. </types>  
  14. <message name="minus">  
  15. <part name="parameters" element="tns:minus"/>  
  16. </message>  
  17. <message name="minusResponse">  
  18. <part name="parameters" element="tns:minusResponse"/>  
  19. </message>  
  20. <message name="add">  
  21. <part name="parameters" element="tns:add"/>  
  22. </message>  
  23. <message name="addResponse">  
  24. <part name="parameters" element="tns:addResponse"/>  
  25. </message>  
  26. <portType name="IMyservice">  
  27. <operation name="minus">  
  28. <input message="tns:minus"/>  
  29. <output message="tns:minusResponse"/>  
  30. </operation>  
  31. <operation name="add">  
  32. <input message="tns:add"/>  
  33. <output message="tns:addResponse"/>  
  34. </operation>  
  35. </portType>  
  36. <binding name="MyServiceImplPortBinding" type="tns:IMyservice">  
  37. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>  
  38. <operation name="minus">  
  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="add">  
  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="MyServiceImplService">  
  58. <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">  
  59. <soap:address location="http://localhost:7777/tudou"/>  
  60. </port>  
  61. </service>  
  62. </definitions>  

 

 

 

下面我们创建客户端访问:

 

[java] view plain copy  print?

  1. package com.whaty.platform.ws.client;  
  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. import com.whaty.platform.ws.server.IMyservice;  
  10.   
  11. /** 
  12.  * @className:MyClient.java 
  13.  * @Desc:访问发布的服务 
  14.  * @author:lizhuang 
  15.  * @createTime:2012-12-21 上午01:23:57 
  16.  */  
  17. public class MyClient {  
  18.     public static void main(String[] args) {  
  19.   
  20.         try {  
  21.             //服务WSDL Document的地址  
  22.             URL url = new URL("http://localhost:7777/tudou?wsdl");  
  23.             //Qnameqname是qualified name 的简写  
  24.             //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成  
  25.             //由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/,  
  26.             QName qname=new QName("http://server.ws.platform.whaty.com/","MyServiceImplService");  
  27.             Service service=Service.create(url, qname);  
  28.             IMyservice ms=service.getPort(IMyservice.class);  
  29.             ms.add(14);  
  30.             ms.minus(14);  
  31.         } catch (MalformedURLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  

 

 

 

控制台打印如下:

 

 

 

a+b=5
a-b=-3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值