当前位置: 首页 > article >正文

SpringBoot整合WebService

目标

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

接口

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

服务端 项目

springBoot+mybatis-plus+webService

结构

在这里插入图片描述

WebServiceConfig

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

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



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

请求

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的调用代码

 wsimport -keep http://127.0.0.1:2334/ws/admin?wsdl

WebServiceClient

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


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

}

http://www.kler.cn/a/418948.html

相关文章:

  • Maya 中创建游戏角色的头发,并将其导出到 Unreal Engine 5
  • MySQL 复合查询
  • Electron builder打包配置
  • javaScript数据类型存储
  • MySQL —— MySQL 程序
  • 【聊天室后端服务器开发】语音转换子服务
  • flink学习(9)——time+water mark
  • Linux服务器使用JupyterLab
  • shell编程基础笔记
  • 相关前端程序
  • element-plus弹窗二次封装踩坑
  • repo仓库转移到自己本地的git服务器
  • PostgreSQL17官网下载详细教程
  • Flutter 指纹识别
  • MVC core 传值session
  • Android 混淆问题
  • Rust vs Java:后端开发应该选哪个?
  • npm 最新国内淘宝镜像地址源 (旧版已不能用)
  • VS Code前端常用插件
  • 使用ECharts创建带百分比标注的环形图
  • ZOOKEEPER [Zookeeper——Docker下安装部署]
  • 解决“ VMware Tools for Windows Vista and later“报错问题
  • 泷羽sec-云技术
  • 机器学习6_支持向量机_算法流程
  • 易速鲜花聊天客服机器人的开发(下)
  • 服务器数据恢复—raid6阵列硬盘被误重组为raid5阵列的数据恢复案例