python调用java编写的Webservice

本文介绍了一个简单的Java WebService示例,包括接口定义、实现类和服务发布,并演示了如何使用Python客户端调用这些服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

webservice的接口代码:

  1. packagecom.xxx.test.ws;
  2. importjavax.jws.WebMethod;
  3. importjavax.jws.WebService;
  4. /**
  5. *CreatedwithIntelliJIDEA.
  6. *User:Administrator
  7. *Date:14-3-5
  8. *Time:下午3:11
  9. */
  10. @WebService(targetNamespace="http://xxx.com/wsdl")
  11. publicinterfaceCalculatorWs{
  12. @WebMethod
  13. publicintsum(intadd1,intadd2);
  14. @WebMethod
  15. publicintmultiply(intmul1,intmul2);
  16. }
实现代码:

  1. packagecom.xxx.test.ws;
  2. importjavax.jws.WebService;
  3. /**
  4. *CreatedwithIntelliJIDEA.
  5. *User:Administrator
  6. *Date:14-3-5
  7. *Time:下午3:12
  8. */
  9. @WebService(
  10. portName="CalculatorPort",
  11. serviceName="CalculatorService",
  12. targetNamespace="http://xxx.com/wsdl",
  13. endpointInterface="com.xxx.test.ws.CalculatorWs")
  14. publicclassCalculatorimplementsCalculatorWs{
  15. publicintsum(intadd1,intadd2){
  16. returnadd1+add2;
  17. }
  18. publicintmultiply(intmul1,intmul2){
  19. returnmul1*mul2;
  20. }
  21. }

发布代码:

  1. packagecom.xxx.test.endpoint;
  2. importcom.xxx.test.ws.Calculator;
  3. importjavax.xml.ws.Endpoint;
  4. /**
  5. *CreatedwithIntelliJIDEA.
  6. *User:Administrator
  7. *Date:14-3-10
  8. *Time:下午3:10
  9. */
  10. publicclassCalclulatorPublisher{
  11. publicstaticvoidmain(String[]args){
  12. Endpoint.publish("http://localhost:8080/test/calc",newCalculator());
  13. //Endpoint.publish("http://10.3.18.44:8080/test/calc",newCalculator());
  14. }
  15. }

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

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

  1. http://localhost:8080/test/calc?wsdl
来验证是否启动成功。

接下来是python的测试代码:

[python] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/python
  2. importsuds
  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. printclient
  8. sum_result=service.sum(10,34)
  9. printsum_result
  10. printclient.last_received()
  11. multiply_result=service.multiply(5,5)
  12. printmultiply_result
  13. printclient.last_received()

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

  1. chmod+xwebservice.py


然后就可以直接执行了:

  1. ./webservice.py

输出结果如下:

  1. Suds(https://fedorahosted.org/suds/)version:0.3.9(beta)build:R658-20100210
  2. Service(CalculatorService)tns="http://xxx.com/wsdl"
  3. Prefixes(1)
  4. ns0="http://xxx.com/wsdl"
  5. Ports(1):
  6. (CalculatorPort)
  7. Methods(2):
  8. multiply(xs:intarg0,xs:intarg1,)
  9. sum(xs:intarg0,xs:intarg1,)
  10. Types(4):
  11. multiply
  12. multiplyResponse
  13. sum
  14. sumResponse
  15. 44
  16. <?xmlversion="1.0"encoding="UTF-8"?>
  17. <S:Envelope>
  18. <S:Body>
  19. <ns2:sumResponse>
  20. <return>44</return>
  21. </ns2:sumResponse>
  22. </S:Body>
  23. </S:Envelope>
  24. 25
  25. <?xmlversion="1.0"encoding="UTF-8"?>
  26. <S:Envelope>
  27. <S:Body>
  28. <ns2:multiplyResponse>
  29. <return>25</return>
  30. </ns2:multiplyResponse>
  31. </S:Body>
  32. </S:Envelope>

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

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

  1. wgethttp://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
  1. tarzxvfsuds-0.3.9.tar.gz
  1. cdsuds-0.3.9
  1. sudopythonsetup.pyinstall
<完工>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值