一、java端
首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。
webservice的接口代码:
代码如下: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);
}
接口实现代码:
代码如下: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;
}
}
发布Webservice代码:[code]
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());
}
}[/code]
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问:
代码如下:http://localhost:8080/test/calc?wsdl
来验证是否启动成功。
二、python端
接下来是python的测试代码:
代码如下:#!/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
新闻热点
疑难解答