首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。
webservice的接口代码:
package jk.kaifa.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
@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);
}
package jk.kaifa.ws;
import javax.jws.WebService;
@WebService(
portName = "CalculatorPort",
serviceName = "CalculatorService",
targetNamespace = "http://xxx.com/wsdl",
endpointInterface = "jk.kaifa.ws.CalculatorWs")
public class Calculator implements CalculatorWs{
@Override
public int sum(int add1, int add2) {
// TODO Auto-generated method stub
return add1 + add2;
}
@Override
public int multiply(int mul1, int mul2) {
// TODO Auto-generated method stub
return mul1 * mul2;
}
}
package jk.kaifa.endpoint;
import javax.xml.ws.Endpoint;
import jk.kaifa.ws.Calculator;
public class CalculatorPublisher {
public static void main(String[] args){
Endpoint.publish("http://localhost:8080/test/calc",
new Calculator());
}
}
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问 http://localhost:8080/test/calc?wsdl 点击打开链接 来验证是否启动成功。
接下来是python的测试代码:
import suds
url = 'http://localhost:8080/test/calc?wsdl'
client = suds.client.Client(url)
service = client.service
print client
sum_result = service.sum(10,34)
multiply_result = service.multiply(2,3)
print sum_result
print multiply_result
print client.last_received()
Suds ( https://fedorahosted.org/suds/ ) version: 0.4 GA build: R699-20100913
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
6
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:multiplyResponse>
<return>6</return>
</ns2:multiplyResponse>
</S:Body>
</S:Envelope>
Traceback (most recent call last):
File "ws.py", line 1, in <module>
import suds
ImportError: No module named suds
easy_install如何使用可以百度一下,这里就不详细讲解了