• SpringBoot集成WebService(wsdl)


    pom.xml

            <dependency>
                <groupId>com.fasterxml.jackson.dataformatgroupId>
                <artifactId>jackson-dataformat-xmlartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.cxfgroupId>
                <artifactId>cxf-spring-boot-starter-jaxwsartifactId>
                
                <version>3.4.4version>
            dependency>
    

    创建入口

    ApplicationContextUtils.java

    bean调用工具

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    /**
     * 创建日期:2024-07-01
     */
    @Component
    public class ApplicationContextUtils implements ApplicationContextAware {
        //构造函数私有化,防止其它人实例化该对象
        private ApplicationContextUtils(){}
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            ApplicationContextUtils.applicationContext = applicationContext;
        }
    
        //通过name获取 Bean.(推荐,因为bean的name是唯一的,出现重名的bean启动会报错。)
        public static Object getBean(String name) {
            return applicationContext.getBean(name);
        }
    
        //通过class获取Bean.(确保bean的name不会重复。因为可能会出现在不同包的同名bean导致获取到2个实例)
        public static <T> T getBean(Class<T> clazz) {
            return applicationContext.getBean(clazz);
        }
    
        //通过name,以及Clazz返回指定的Bean(这个是最稳妥的)
        public static <T> T getBean(String name, Class<T> clazz) {
            return applicationContext.getBean(name, clazz);
        }
    
    }
    

    IWebService.java

    统一入口

    public interface IWebService {
    
        String handle(String parameter);
    
    }
    

    WebServiceEntry.java

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    /**
     * 创建日期:2024-07-01
     */
    @Slf4j
    @Service
    @WebService
    public class WebServiceEntry {
    	/**
         * 反射调用方法
         * @param service bean name
         * @param parameter 请求参数字符串,一般是xml格式的字符串
         */
        @WebMethod
        public String invoke(@WebParam(name = "service") String service,
                      @WebParam(name = "parameter") String parameter) {
            IWebService webService = (IWebService) ApplicationContextUtils.getBean(service);
            return webService.handle(parameter);
        }
    
    }
    

    WebServiceConfig.java

    配置类
    有些依赖千万不要导错,所以我依赖都粘贴进来了。防止导错包。

    import org.apache.cxf.Bus;
    import org.apache.cxf.bus.spring.SpringBus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.apache.cxf.transport.servlet.CXFServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.xml.ws.Endpoint;
    
    /**
     * 创建日期:2024-07-01
     */
    @Configuration
    public class WebServiceConfig {
    
        @Bean(name = "cxfServlet")
        public ServletRegistrationBean<?> cxfServlet() {
            //urlMappings默认是:services
            return new ServletRegistrationBean<>(new CXFServlet(), "/services/*");
        }
    
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }
    
        @Bean
        public Endpoint helloServiceEndpoint() {
            EndpointImpl endpoint = new EndpointImpl(springBus(), new WebServiceEntry());
            //services后面的uri地址
            endpoint.publish("/WebServiceEntry");
            return endpoint;
        }
    
    }
    

    WebMvcConfig.java

    web的配置类,因为增加了xml依赖,springboot会默认把json放到xml后面,因此要手动改回默认json。

    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
    
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            //引入 jackson-dataformat-xml 后,原本默认返回json变成了默认返回xml。因此这里要设置默认返回json
            configurer.defaultContentType(MediaType.APPLICATION_JSON);
        }
    
    }
    

    实现IWebService接口

    如:WebServiceImpl

    @Service("Hello")
    public class Hello implements IWebService {
    
        @Override
        public String handle(String parameter) {
            return "hello";
        }
    
    }
    

    启动SpringBoot

    访问

    http://localhost:8080/services/WebServiceEntry?wsdl
    

    会出现如下所示界面
    在这里插入图片描述
    soapUI去调用接口
    ws="http://ws.bsjkt.bsoft.com/"这里每个人可能不一样
    service=bean name
    parameter=请求参数

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.bsjkt.bsoft.com/">
    <soapenv:Header/>
    <soapenv:Body>
    <ws:invoke>
    <service>Hello</service>
    <parameter><![CDATA[
    <Data>
    </Data>
    ]]></parameter>
          </ws:invoke>
       </soapenv:Body>
    </soapenv:Envelope>
    
  • 相关阅读:
    Learn-1
    【Redis】深入探索 Redis 的数据类型 —— 列表 List
    跳过开屏广告
    vue,mixins混入
    在各种开发项目中使用公用类库的扩展方法,通过上下文方式快速调用处理函数
    Redis(十三)【集群基础搭建】
    《乔布斯传》英文原著重点词汇笔记(四)【 chapter two 】
    文件上传漏洞
    如何用 Dall-E2 制作这些超可爱的柔软毛绒玩具
    idea 打开文件一直loading
  • 原文地址:https://blog.csdn.net/weixin_43933728/article/details/140109357