什么是 webservice
WebService是一种跨编程语言、跨操作系统平台的远程调用技术。
server 端:提供服务
client 端:调用服务
示例
Server 端
因为是基于 Springboot 创建的,所以需要 springboot 的依赖,这里省略了,只写相关主要代码以及依赖
- pom.xml
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
- 配置类
配置注册相应的 bean,并且发布出去
@Configuration
public class CxfConfig {
// 默认servlet路径 /*,如果覆写则按照自己定义的来,注册一个 CXFServlet
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
// 把实现类交给spring管理
@Bean
public IService appService() {
return new UserServiceImpl();
}
// 终端路径
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), appService());
endpoint.getInInterceptors().add(new AuthInterceptor());// 添加校验拦截器
// 发布
endpoint.publish("/user");
return endpoint;
}
}
- 定义接口类(也就是提供出去的服务)
@WebService(targetNamespace = "http://service.beng.com/")
public interface IService {
@WebMethod
String getUserName(@WebParam(name = "id") String id) throws UnsupportedEncodingException;
public User getUserNameById(String id) throws UnsupportedEncodingException;
@WebMethod
public User getUser(String id) throws UnsupportedEncodingException;
}
- 接口实现类
定义接口服务的具体实现逻辑
@WebService(name = "UserService", targetNamespace = "http://service.beng.com/", endpointInterface = "com.beng.service.IService")
public class UserServiceImpl implements IService {
@Override
public String getUserName(String id) throws UnsupportedEncodingException {
System.out.print("=========>>>>>>>>>" + id);
// TODO 调用数据库(查询数据库业务逻辑)
User user = User.builder().id(1).username("小机灵鬼").age(18).build();
return JSONObject.toJSONString(user);
}
@Override
public User getUserNameById(String id) throws UnsupportedEncodingException {
System.out.print("=========>>>>>>>>>" + id);
// TODO 调用数据库(查询数据库业务逻辑)
return User.builder().id(1).username("小机灵鬼").age(18).build();
}
@Override
public User getUser(String id) throws UnsupportedEncodingException {
System.out.print("=========>>>>>>>>>" + id);
// TODO 调用数据库(查询数据库业务逻辑)
return User.builder().id(1).username("小机灵鬼").age(18).build();
}
}
- 启动后访问 http://localhost:8888/services/user?wsdl
到此 server 端就启动完成了。
Client 端
客户端有两种调用当时:
- 不需要将服务端的代码copy过来
public class CxfDynamicClient {
private static String address = "http://localhost:8888/services/user?wsdl";
public static void invokeRemoteService() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(address);
// 需要密码的情况需要加上用户名和密码
client.getOutInterceptors().add(new LoginInterceptor("root", "admin"));
Object[] objects = new Object[0];
try {
System.out.println("======client" + client);
objects = client.invoke("getUserNameById", "1");
User user = new User();
BeanUtils.copyProperties((objects[0]), user);
System.out.println("返回数据:" + user.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
invokeRemoteService();
}
- 需要将服务员的接口copy过来
public class CxfDefaultClient {
private static String address = "http://localhost:8888/services/user?wsdl";
public static void invokeRemoteService() {
try {
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 添加用户名密码拦截器
jaxWsProxyFactoryBean.getOutInterceptors().add(new LoginInterceptor("root", "admin"));
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(IService.class);
// 创建一个代理接口实现
IService cs = (IService) jaxWsProxyFactoryBean.create();
// 数据准备
String LineId = "1";
// 调用代理接口的方法调用并返回结果
User result = (User) cs.getUser(LineId);
System.out.println("==============返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
invokeRemoteService();
}
}
github 地址:https://github.com/abinbao/spring-bucket/tree/master/springboot-webservice