• 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>
    
  • 相关阅读:
    无公网IP通过旁路由openwrt的Zerotier实现和在家一样访问家里每个设备
    Android常见分区
    Spring IOC 01
    力扣 667. 优美的排列 II
    opencv创建窗口—cv.namedWindow()
    计算机丢失mfc100.dll如何恢复,详细解析mfc100.dll文件丢失解决方法
    cmake使用教程
    R语言使用lead函数将dataframe数据向前移动指定的行数(尾部补NA值)
    【5G PHY】5G SS/PBCH块介绍(一)
    存储 MD5 的值应该用 VARCHAR 还是 CHAR
  • 原文地址:https://blog.csdn.net/weixin_43933728/article/details/140109357