springBoot如何快速发布webService接口?(含测试工具)

2023-12-13 04:03:43

文章目录

  1. 引入maven依赖

org.apache.cxf cxf-rt-frontend-jaxws 3.4.5 org.apache.cxf cxf-rt-transports-http 3.4.5 org.apache.cxf cxf-spring-boot-starter-jaxws 3.4.5 ```
  1. 新建webService接口
    注意接口要添加注释@WebService,且要添加name和targetNamespace属性

    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    @WebService(name = "likuWmsWsService",targetNamespace = "http://impl.service.system.ruoyi.com/")
    public interface IWmsTestService {
    
        @WebMethod
        public String queryStock(@WebParam(name = "param") String param);
    }
    
  2. 增加接口实现类

    @WebService
    @Configuration
    public class WmsTestServiceImpl implements IWmsTestService {
    
        @Override
        public String queryStock(String param) {
            System.out.printf("接收的参数:"+param);
            Map<String, Object> returnMap = new HashMap<String, Object>();
            returnMap.put("data",new ArrayList<>());
            returnMap.put("MSGTY","S");
            returnMap.put("MSGTX","失败原因");
            return JSONObject.toJSONString(returnMap);
        }
    }
    
  3. 增加发布webService的类

    import com.ruoyi.system.service.IWmsTestService;
    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.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import javax.xml.ws.Endpoint;
    
    @Configuration
    public class WebServicePublish {
        @Autowired
        private Bus bus;
        @Autowired
        private IWmsTestService wmsTestServiceImpl;
    
        /**
         * 用于注册CXFServlet的
         * 地址/webservice/*
         * * 通配符 更上接口地址
         *
         */
        @Bean("cxfServletRegistration")
        public ServletRegistrationBean dispatcherServlet() {
            // 这里就是发布服务的跟路径,后面 * 是通配符,表示跟什么都行
            return new ServletRegistrationBean<>(new CXFServlet(), "/webservice/*");
        }
    
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }
    
        @Bean
        public Endpoint endpoint() {
            // 建立一个端点 ,第一个参数是 springBus 对象,第二个参数是刚才的接口实现类(因为在实现类中用了@service,所以这里可以自动注入)
            // PS: 要是有多个service,这个方法对象多写几个就行
            EndpointImpl endpoint = new EndpointImpl(bus, wmsTestServiceImpl);
            // 这里就是发布的这个接口的地址
            endpoint.publish("/wmsWs");
            return endpoint;
        }
    }
    
  4. 服务启动成功后,通过浏览器访问wsdl地址

    localhost:8083/webservice/wmsWs?wsdl

    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.system.ruoyi.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WmsTestServiceImplService" targetNamespace="http://impl.service.system.ruoyi.com/">
    <wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.system.ruoyi.com/" elementFormDefault="unqualified" targetNamespace="http://impl.service.system.ruoyi.com/" version="1.0">
    <xs:element name="queryStock" type="tns:queryStock"/>
    <xs:element name="queryStockResponse" type="tns:queryStockResponse"/>
    <xs:complexType name="queryStock">
    <xs:sequence>
    <xs:element minOccurs="0" name="param" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="queryStockResponse">
    <xs:sequence>
    <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:schema>
    </wsdl:types>
    <wsdl:message name="queryStock">
    <wsdl:part element="tns:queryStock" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="queryStockResponse">
    <wsdl:part element="tns:queryStockResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="likuWmsWsService">
    <wsdl:operation name="queryStock">
    <wsdl:input message="tns:queryStock" name="queryStock"> </wsdl:input>
    <wsdl:output message="tns:queryStockResponse" name="queryStockResponse"> </wsdl:output>
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="WmsTestServiceImplServiceSoapBinding" type="tns:likuWmsWsService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="queryStock">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="queryStock">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="queryStockResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="WmsTestServiceImplService">
    <wsdl:port binding="tns:WmsTestServiceImplServiceSoapBinding" name="WmsTestServiceImplPort">
    <soap:address location="http://localhost:8083/webservice/wmsWs"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    
  5. 通过webServiceStudio工具进行调试

    工具下载地址:https://pan.baidu.com/s/1F5WgMPM3u4yY-xgYyskb8Q?pwd=yxao
    调用测试:
    在这里插入图片描述

在这里插入图片描述

文章来源:https://blog.csdn.net/LT_1029/article/details/134833213
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。