python调用java编写的Webservice
2016-07-13 15:51
573 查看
首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。
webservice的接口代码:
[java] view
plain copy
print?
package com.xxx.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* Time: 下午3:11
*/
@WebService(targetNamespace = "http://xxx.com/wsdl")
public interface CalculatorWs {
@WebMethod
public int sum(int add1, int add2);
@WebMethod
public int multiply(int mul1, int mul2);
}
实现代码:
[java] view
plain copy
print?
package com.xxx.test.ws;
import javax.jws.WebService;
/**
* Created with IntelliJ IDEA.
* 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")
public class Calculator implements CalculatorWs {
public int sum(int add1, int add2) {
return add1 + add2;
}
public int multiply(int mul1, int mul2) {
return mul1 * mul2;
}
}
发布代码:
[java] view
plain copy
print?
package com.xxx.test.endpoint;
import com.xxx.test.ws.Calculator;
import javax.xml.ws.Endpoint;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-10
* Time: 下午3:10
*/
public class CalclulatorPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/test/calc", new Calculator());
//Endpoint.publish("http://10.3.18.44:8080/test/calc", new Calculator());
}
}
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问
[java] view
plain copy
print?
http://localhost:8080/test/calc?wsdl
来验证是否启动成功。
接下来是python的测试代码:
[python] view
plain copy
print?
#!/usr/bin/python
import suds
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
print client
sum_result = service.sum(10, 34)
print sum_result
print client.last_received()
multiply_result = service.multiply(5, 5)
print multiply_result
print client.last_received()
将上述代码保存成webservice.py文件,再修改一下可执行权限:
[plain] view
plain copy
print?
chmod +x webservice.py
然后就可以直接执行了:
[plain] view
plain copy
print?
./webservice.py
输出结果如下:
[plain] view
plain copy
print?
Suds ( https://fedorahosted.org/suds/ ) version: 0.3.9 (beta) build: R658-20100210
Service ( CalculatorService ) tns="http://xxx.com/wsdl"
Prefixes (1)
ns0 = "http://xxx.com/wsdl"
Ports (1):
(CalculatorPort)
Methods (2):
multiply(xs:int arg0, xs:int arg1, )
sum(xs:int arg0, xs:int arg1, )
Types (4):
multiply
multiplyResponse
sum
sumResponse
44
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:sumResponse>
<return>44</return>
</ns2:sumResponse>
</S:Body>
</S:Envelope>
25
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:multiplyResponse>
<return>25</return>
</ns2:multiplyResponse>
</S:Body>
</S:Envelope>
注意,执行上面的代码时,有可能提示
[python] view
plain copy
print?
Traceback (most recent call last):
File "ws.py", line 1, in <module>
import suds
ImportError: No module named suds
说缺少依赖的包,我们可以手工下载安装suds包。
[plain] view
plain copy
print?
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?
tar zxvf suds-0.3.9.tar.gz
[plain] view
plain copy
print?
cd suds-0.3.9
[plain] view
plain copy
print?
sudo python setup.py install
<完工>
webservice的接口代码:
[java] view
plain copy
print?
package com.xxx.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* Time: 下午3:11
*/
@WebService(targetNamespace = "http://xxx.com/wsdl")
public interface CalculatorWs {
@WebMethod
public int sum(int add1, int add2);
@WebMethod
public int multiply(int mul1, int mul2);
}
实现代码:
[java] view
plain copy
print?
package com.xxx.test.ws;
import javax.jws.WebService;
/**
* Created with IntelliJ IDEA.
* 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")
public class Calculator implements CalculatorWs {
public int sum(int add1, int add2) {
return add1 + add2;
}
public int multiply(int mul1, int mul2) {
return mul1 * mul2;
}
}
发布代码:
[java] view
plain copy
print?
package com.xxx.test.endpoint;
import com.xxx.test.ws.Calculator;
import javax.xml.ws.Endpoint;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-10
* Time: 下午3:10
*/
public class CalclulatorPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/test/calc", new Calculator());
//Endpoint.publish("http://10.3.18.44:8080/test/calc", new Calculator());
}
}
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问
[java] view
plain copy
print?
http://localhost:8080/test/calc?wsdl
来验证是否启动成功。
接下来是python的测试代码:
[python] view
plain copy
print?
#!/usr/bin/python
import suds
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
print client
sum_result = service.sum(10, 34)
print sum_result
print client.last_received()
multiply_result = service.multiply(5, 5)
print multiply_result
print client.last_received()
将上述代码保存成webservice.py文件,再修改一下可执行权限:
[plain] view
plain copy
print?
chmod +x webservice.py
然后就可以直接执行了:
[plain] view
plain copy
print?
./webservice.py
输出结果如下:
[plain] view
plain copy
print?
Suds ( https://fedorahosted.org/suds/ ) version: 0.3.9 (beta) build: R658-20100210
Service ( CalculatorService ) tns="http://xxx.com/wsdl"
Prefixes (1)
ns0 = "http://xxx.com/wsdl"
Ports (1):
(CalculatorPort)
Methods (2):
multiply(xs:int arg0, xs:int arg1, )
sum(xs:int arg0, xs:int arg1, )
Types (4):
multiply
multiplyResponse
sum
sumResponse
44
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:sumResponse>
<return>44</return>
</ns2:sumResponse>
</S:Body>
</S:Envelope>
25
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:multiplyResponse>
<return>25</return>
</ns2:multiplyResponse>
</S:Body>
</S:Envelope>
注意,执行上面的代码时,有可能提示
[python] view
plain copy
print?
Traceback (most recent call last):
File "ws.py", line 1, in <module>
import suds
ImportError: No module named suds
说缺少依赖的包,我们可以手工下载安装suds包。
[plain] view
plain copy
print?
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?
tar zxvf suds-0.3.9.tar.gz
[plain] view
plain copy
print?
cd suds-0.3.9
[plain] view
plain copy
print?
sudo python setup.py install
<完工>
相关文章推荐
- 廖雪峰python学习笔记10&&11:容错处理与文件读写
- Python字符串的内置方法
- Python列表的内置方法
- Python——使用__future__遇见的坑
- Python之集合的用法
- python学习笔记之module && package
- Python 字符编码判断
- python实现 Excel 转为json
- python 读文件 写文件 字符串操作 LIBSVM格式数据转换
- Python字典的内置方法
- Python 抓图
- python 重新执行循环中出错的那一次
- python 文件操作
- python的requests初步使用
- Python 开发环境
- python 文件操作
- python笔记---OS模块内建方法
- dataTable实现服务器端分页(python)
- Python基础篇-Python基础进阶
- Python之函数参数介绍