python调用java的Webservice示例
一、java端
首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。
webservice的接口代码:
importjavax.jws.WebMethod;
importjavax.jws.WebService;
/**
*CreatedwithIntelliJIDEA.
*User:Administrator
*Date:14-3-5
*Time:下午3:11
*/
@WebService(targetNamespace="http://xxx.com/wsdl")
publicinterfaceCalculatorWs{
@WebMethod
publicintsum(intadd1,intadd2);
@WebMethod
publicintmultiply(intmul1,intmul2);
}
接口实现代码:
importjavax.jws.WebService;
/**
*CreatedwithIntelliJIDEA.
*User:Administrator
*Date:14-3-5
*Time:下午3:12
*/
@WebService(
portName="CalculatorPort",
serviceName="CalculatorService",
targetNamespace="http://xxx.com/wsdl",
endpointInterface="com.xxx.test.ws.CalculatorWs")
publicclassCalculatorimplementsCalculatorWs{
publicintsum(intadd1,intadd2){
returnadd1+add2;
}
publicintmultiply(intmul1,intmul2){
returnmul1*mul2;
}
}
发布Webservice代码:[code]
packagecom.xxx.test.endpoint;
importcom.xxx.test.ws.Calculator;
importjavax.xml.ws.Endpoint;
/**
*CreatedwithIntelliJIDEA.
*User:Administrator
*Date:14-3-10
*Time:下午3:10
*/
publicclassCalclulatorPublisher{
publicstaticvoidmain(String[]args){
Endpoint.publish("http://localhost:8080/test/calc",newCalculator());
//Endpoint.publish("http://10.3.18.44:8080/test/calc",newCalculator());
}
}[/code]
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问:
来验证是否启动成功。
二、python端
接下来是python的测试代码:
importsuds
url="http://localhost:8080/test/calc?wsdl"
#url="http://10.3.18.44:8080/test/calc?wsdl"
client=suds.client.Client(url)
service=client.service
printclient
sum_result=service.sum(10,34)
printsum_result
printclient.last_received()
multiply_result=service.multiply(5,5)
printmultiply_result
printclient.last_received()
将上述代码保存成webservice.py文件,再修改一下可执行权限:
输出结果如下:
Service(CalculatorService)tns="http://xxx.com/wsdl"
Prefixes(1)
ns0="http://xxx.com/wsdl"
Ports(1):
(CalculatorPort)
Methods(2):
multiply(xs:intarg0,xs:intarg1,)
sum(xs:intarg0,xs:intarg1,)
Types(4):
multiply
multiplyResponse
sum
sumResponse
44
<?xmlversion="1.0"encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:sumResponse>
<return>44</return>
</ns2:sumResponse>
</S:Body>
</S:Envelope>
25
<?xmlversion="1.0"encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:multiplyResponse>
<return>25</return>
</ns2:multiplyResponse>
</S:Body>
</S:Envelope>
三、常见问题
注意,在执行上面的代码时,有可能提示:
File"ws.py",line1,in<module>
importsuds
ImportError:Nomodulenamedsuds
说缺少依赖的包,我们可以手工下载安装suds包。
tarzxvfsuds-0.3.9.tar.gz
cdsuds-0.3.9
sudopythonsetup.pyinstall
OK。
相关文章
- python中bool函数_bool()函数以及Python中的示例
- java backoff_Java BackOff类代码示例
- java传真发送,用Java发送传真解决之道
- java源程序文件扩展名_JAVA源代码的扩展名为( )
- 怎么下载java_怎样下载Java的环境?
- Python 技巧篇-pip卸载python库实例演示,查看pip命令大全方法[通俗易懂]
- python pkl文件_Python字符串格式化输出的方式包括
- 10个Python脚本来自动化你的日常任务
- java prototype是什么,Java设计模式之原型模式(Prototype模式)介绍
- 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法
- Python字典的基本概念_Python自学第二十六节
- Python 自动化系统信息收集
- Python 内置界面开发框架 Tkinter入门篇 乙
- python shell 根据 ip 获取 hostname详解编程语言
- java JNI: C 语言调用 Java 方法示例详解编程语言
- Python 算法(2) 哈夫曼编码 Huffman Encoding详解编程语言
- 玩转Linux:Java开发入门指南(linux上开发java)
- 面试前准备:Java技术和Redis快速入门(java面试redis)
- Java如何启动MySQL?(java启动mysql)
- Connecting to MSSQL with Python: A Comprehensive Guide for Beginners(python连接mssql)
- 期Java开发高效配置MySQL长期支撑(mysql java 长)
- 在Linux上实现Java程序的运行(linux运行java程序)
- Python获取电脑硬件信息及状态的实现方法