python调用java编写的Webservice

首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。

webservice的接口代码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xxx.test.ws;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5.   
  6. /** 
  7.  * Created with IntelliJ IDEA. 
  8.  * User: Administrator 
  9.  * Date: 14-3-5 
  10.  * Time: 下午3:11 
  11.  */  
  12. @WebService(targetNamespace = "http://xxx.com/wsdl")  
  13. public interface CalculatorWs {  
  14.     @WebMethod  
  15.     public int sum(int add1, int add2);  
  16.   
  17.     @WebMethod  
  18.     public int multiply(int mul1, int mul2);  
  19. }  
实现代码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xxx.test.ws;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /** 
  6.  * Created with IntelliJ IDEA. 
  7.  * User: Administrator 
  8.  * Date: 14-3-5 
  9.  * Time: 下午3:12 
  10.  */  
  11. @WebService(  
  12.         portName = "CalculatorPort",  
  13.         serviceName = "CalculatorService",  
  14.         targetNamespace = "http://xxx.com/wsdl",  
  15.         endpointInterface = "com.xxx.test.ws.CalculatorWs")  
  16. public class Calculator implements CalculatorWs {  
  17.     public int sum(int add1, int add2) {  
  18.         return add1 + add2;  
  19.     }  
  20.   
  21.     public int multiply(int mul1, int mul2) {  
  22.         return mul1 * mul2;  
  23.     }  
  24. }  

发布代码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xxx.test.endpoint;  
  2.   
  3. import com.xxx.test.ws.Calculator;  
  4.   
  5. import javax.xml.ws.Endpoint;  
  6.   
  7. /** 
  8.  * Created with IntelliJ IDEA. 
  9.  * User: Administrator 
  10.  * Date: 14-3-10 
  11.  * Time: 下午3:10 
  12.  */  
  13. public class CalclulatorPublisher {  
  14.     public static void main(String[] args) {  
  15.         Endpoint.publish("http://localhost:8080/test/calc"new Calculator());  
  16.         //Endpoint.publish("http://10.3.18.44:8080/test/calc", new Calculator());  
  17.     }  
  18. }  

运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。

上面的代码跑起来后,你可以直接使用浏览器访问

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. http://localhost:8080/test/calc?wsdl  
来验证是否启动成功。

接下来是python的测试代码:

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/python  
  2. import suds  
  3. url = 'http://localhost:8080/test/calc?wsdl'  
  4. #url = 'http://10.3.18.44:8080/test/calc?wsdl'  
  5. client = suds.client.Client(url)  
  6. service = client.service  
  7.   
  8. print client  
  9.   
  10. sum_result = service.sum(1034)  
  11. print sum_result  
  12. print client.last_received()  
  13.   
  14. multiply_result = service.multiply(55)  
  15. print multiply_result  
  16. print client.last_received()  

将上述代码保存成webservice.py文件,再修改一下可执行权限:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. chmod +x webservice.py  


然后就可以直接执行了:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./webservice.py  

输出结果如下:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. Suds ( https://fedorahosted.org/suds/ )  version: 0.3.9 (beta)  build: R658-20100210  
  2.   
  3. Service ( CalculatorService ) tns="http://xxx.com/wsdl"  
  4.    Prefixes (1)  
  5.       ns0 = "http://xxx.com/wsdl"  
  6.    Ports (1):  
  7.       (CalculatorPort)  
  8.          Methods (2):  
  9.             multiply(xs:int arg0, xs:int arg1, )  
  10.             sum(xs:int arg0, xs:int arg1, )  
  11.          Types (4):  
  12.             multiply  
  13.             multiplyResponse  
  14.             sum  
  15.             sumResponse  
  16.   
  17.   
  18. 44  
  19. <?xml version="1.0" encoding="UTF-8"?>  
  20. <S:Envelope>  
  21.    <S:Body>  
  22.       <ns2:sumResponse>  
  23.          <return>44</return>  
  24.       </ns2:sumResponse>  
  25.    </S:Body>  
  26. </S:Envelope>  
  27. 25  
  28. <?xml version="1.0" encoding="UTF-8"?>  
  29. <S:Envelope>  
  30.    <S:Body>  
  31.       <ns2:multiplyResponse>  
  32.          <return>25</return>  
  33.       </ns2:multiplyResponse>  
  34.    </S:Body>  
  35. </S:Envelope>  

注意,执行上面的代码时,有可能提示

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. Traceback (most recent call last):  
  2.   File "ws.py", line 1in <module>  
  3.     import suds  
  4. ImportError: No module named suds  
说缺少依赖的包,我们可以手工下载安装suds包。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. wget http://downloads.sourceforge.net/project/python-suds/suds/0.3.9/suds-0.3.9.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpython-suds%2Ffiles%2F&ts=1394436413&use_mirror=nchc  
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. tar zxvf suds-0.3.9.tar.gz  
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. cd suds-0.3.9  
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. sudo python setup.py install  
<完工>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值