SpringBoot整合WebService

目标

暴露相关接口给第三方对接

接口

人员列表接口:支持按照姓名进行模糊搜索

服务端 项目

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);

}
相关推荐
fs哆哆23 分钟前
ExcelVBA判断用户选择区域是否整行或整列
java·开发语言·前端·javascript·ecmascript
phenomenal991 小时前
微服务-01【续】
java·微服务·架构
LG.YDX2 小时前
计算机组成原理课后习题答案:第一章
java·开发语言
moskidi2 小时前
Web day11 SpringBoot原理
java·spring boot·maven
ssxueyi3 小时前
Java 常见Exception异常解决方法
java·开发语言·数据库·异常处理·exception
xzq_java3 小时前
Javafx.麦当劳点餐系统(Java简洁版)
java·sql
silver6873 小时前
jvm内存优化
java
lzz的编码时刻4 小时前
观察者模式:事件处理机制与松耦合设计
java·开发语言·设计模式
yun_shui_4 小时前
【力扣-KMP】28.找出字符串第一个匹配项的下标
java·数据结构·算法·leetcode
lvyuanj4 小时前
IDEA skywalking 启动报错 ClassNotFoundException InstanceConstructorInterceptor
java·intellij-idea·skywalking