分布式通信框架-基于java实现 webservice(分布式六 上)

版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/86024334

服务端代码

实现思路

  1. 提供接口、且类使用注解@WebService,方法使用注解@WebMethod
  2. 实现接口、且类使用注解@WebService
  3. 服务端对外提供服务并绑定具体实现,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);
	}
}

测试

在这里插入图片描述

调用过程类似如下

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/86024334