java调用webservice接口
- Java端写一个webservice接口,如下:
package testNG;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class MyService {
public String sayHello( int intsmaze){
System.out.println("sayHello()...");
return "hello " + intsmaze;
}
public String sayHello2(String intsmaze){
return "hello " + intsmaze;
}
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:6789/hello", new MyService());
//这个地方其实就是进行了封装,里面根据指定参数启动了一个servicesocket,并且生成了一个WSDL文档。
System.out.println("Server ready...");
}
}
2、启动服务
3、通过URL访问http://127.0.0.1:6789/hello?wsdl获取文档
4、在Java/bin下使用wsimport.exe工具编译wsdl文档成Java代码
5、将生成的代码放入idea中
6、更改MyService接口中的类名
7、编写测试代码
package Test2;
public class App {
public static void main(String[] args) {
MyServiceService hss = new MyServiceService();
MyService soap = hss.getMyServicePort();
// String str = soap.sayHello(33);//这里我们看视乎在调用我们本地的方法,其实内部把发送数据组装为soap协议,
String str2 = soap.sayHello2("33");//这里我们看视乎在调用我们本地的方法,其实内部把发送数据组装为soap协议,
// System.out.println(str);
System.out.println(str2);
}
}
8、运行测试代码,如图成功返回数据