版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/86024334
服务端代码
实现思路
- 提供接口、且类使用注解@WebService,方法使用注解@WebMethod
- 实现接口、且类使用注解@WebService
- 服务端对外提供服务并绑定具体实现,Endpoint.publish(“http://localhost:8888/ws/hello”, new SayHelloImpl());
具体代码实现
接口
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface ISayHello {
// SEI,发布出去的接口
@WebMethod
String sayHello(String name);
}
实现
@WebService
public class SayHelloImpl implements ISayHello {
@Override
public String sayHello(String name) {
System.out.println("服务端应到中...)");
return "hello, " + name + ", 我是 张三";
}
}
启动服务端
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/ws/hello", new SayHelloImpl());
System.out.println("服务端启动完成...");
}
}
启动服务端
浏览器中输入: http://localhost:8888/ws/hello?wsdl
使用soapui工具验证发布的服务
新建一个soap工程
输入入参,发起调用
基于wsimport生成代码的客户端
执行生成代码命令
wsimport -keep -p priv.dengjili.distributed_s1.webservice_client http://localhost:8888/ws/hello?wsdl
生成文件
删除多余的文件
编写客户端
import priv.dengjili.distributed_s1.webservice_client.SayHelloImpl;
import priv.dengjili.distributed_s1.webservice_client.SayHelloImplService;
public class Client {
public static void main(String[] args) {
SayHelloImplService service = new SayHelloImplService();
SayHelloImpl sayHello = service.getSayHelloImplPort();
String message = sayHello.sayHello("王阿姨");
System.out.println(message);
}
}
测试
调用过程类似如下