目标
暴露相关接口给第三方对接
接口
人员列表接口:支持按照姓名进行模糊搜索
服务端 项目
springBoot+mybatis-plus+webService
结构
WebServiceConfig
java
import org.gin.webservice.AdminService;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Resource
private AdminService adminService;
/**
* 使用 JAX-WS Endpoint 发布服务
*/
@PostConstruct
public void publishEndpoint() {
String address = "http://localhost:2334/ws/admin"; // 服务发布地址
Endpoint.publish(address, adminService); // 发布服务
}
}
AdminService
java
import org.gin.entity.MainAdmin;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;
@WebService(
name = "AdminService",
serviceName = "AdminService",
endpointInterface = "org.gin.webservice.AdminService",
targetNamespace = "http://webservice.gin.org/")
public interface AdminService {
@WebMethod
public List<MainAdmin> getAdmins(@WebParam(name = "name")String name);
@WebMethod
MainAdmin getAdminById(@WebParam(name = "id")Long id);
}
AdminServiceImpl
java
@Slf4j
@Service
@WebService(
name = "AdminService",
serviceName = "AdminService",
endpointInterface = "org.gin.webservice.AdminService",
targetNamespace = "http://webservice.gin.org/")
public class AdminServiceImpl implements AdminService {
@Resource
private MainAdminMapper mainAdminMapper;
@Override
public List<MainAdmin> getAdmins(String name) {
log.info("name:" + name);
return mainAdminMapper.selectList(
Wrappers.lambdaQuery(MainAdmin.class)
.like(name != null && !name.isEmpty(), MainAdmin::getName, "%" + name + "%")
);
}
@Override
public MainAdmin getAdminById(Long id) {
return mainAdminMapper.selectById(id);
}
}
请求
shell
curl --location --request POST 'http://127.0.0.1:2334/ws/admin' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: text/xml' \
--data-raw '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://webservice.gin.org/">
<soapenv:Header/>
<soapenv:Body>
<ws:getAdmins>
<name>超级</name>
</ws:getAdmins>
</soapenv:Body>
</soapenv:Envelope>'
响应
客户端
结构
可使用wsimport工具生成webservice的调用代码
shell
wsimport -keep http://127.0.0.1:2334/ws/admin?wsdl
WebServiceClient
java
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.stereotype.Service;
@Service
@Getter
@Slf4j
public class WebServiceClient {
private final AdminService adminService;
public WebServiceClient() {
this.adminService = (AdminService) callService(
"http://127.0.0.1:2334/ws/admin?wsdl",
AdminService.class
);;
}
/**
* 调用服务,生成客户端的服务代理
*
* @param address WebService的URL
* @param serviceClass 服务接口的Class
* @return 服务代理对象
*/
private static Object callService(String address, Class<?> serviceClass) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// 记录入站消息
factory.getInInterceptors().add(new LoggingInInterceptor());
// 记录出站消息
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(serviceClass);
factory.setAddress(address);
try {
return factory.create();
} catch (Exception e) {
log.error("WebService调用失败: {}", e.getMessage(), e);
throw new RuntimeException("无法创建服务代理", e);
}
}
}
AdminService
java
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebService(name = "AdminService", targetNamespace = "http://webservice.gin.org/")
public interface AdminService {
/**
*
* @param id
* @return
* returns org.gin.webservice.MainAdmin
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "getAdminById", targetNamespace = "http://webservice.gin.org/", className = "org.gin.webservice.GetAdminById")
@ResponseWrapper(localName = "getAdminByIdResponse", targetNamespace = "http://webservice.gin.org/", className = "org.gin.webservice.GetAdminByIdResponse")
@Action(input = "http://webservice.gin.org/AdminService/getAdminByIdRequest", output = "http://webservice.gin.org/AdminService/getAdminByIdResponse")
public MainAdmin getAdminById(
@WebParam(name = "id", targetNamespace = "")
Long id);
/**
*
* @param name
* @return
* returns java.util.List<org.gin.webservice.MainAdmin>
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "getAdmins", targetNamespace = "http://webservice.gin.org/", className = "org.gin.webservice.GetAdmins")
@ResponseWrapper(localName = "getAdminsResponse", targetNamespace = "http://webservice.gin.org/", className = "org.gin.webservice.GetAdminsResponse")
@Action(input = "http://webservice.gin.org/AdminService/getAdminsRequest", output = "http://webservice.gin.org/AdminService/getAdminsResponse")
public List<MainAdmin> getAdmins(
@WebParam(name = "name", targetNamespace = "")
String name);
}