美文网首页springboot
Spring Boot2.X 发布 WebService

Spring Boot2.X 发布 WebService

作者: 食尘者 | 来源:发表于2019-05-10 15:17 被阅读135次

    环境说明

    JDK:jdk1.8.0_45

    Spring Boot:2.0.1.RELEASE

    cxf-spring-boot-starter-jaxws:3.2.5

    集成过程

    1.添加Maven依赖

    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
       <version>3.2.5</version>
    </dependency>
    

    2.Demo服务示例

    接口:WSDemoService.java

    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    /**
     * Web Service Demo服务接口
     * 
     * @author swordshake
     * @since 1.0
     */
    @WebService
    public interface WSDemoService {
      @WebMethod
      String hello(@WebParam(name = "friend") String friend);
    }
    

    实现:WSDemoServiceImpl.java

    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;
    
    import javax.jws.WebService;
    
    /**
     * Web Service Demo服务实现。
     * 
     * @author swordshake
     * @since 1.0
     */
    @Service
    @WebService(name = "demoservice", targetNamespace = "http://ws.demo.XX.com")
    public class WSDemoServiceImpl implements WSDemoService {
      @Override
      public String hello(String friend) {
        if (StringUtils.isBlank(friend)) {
          return "Who are you ?";
        }
        return "Hello " + friend + "!";
      }
    }
    

    3.注册发布服务

    import org.apache.cxf.Bus;
    import org.apache.cxf.bus.spring.SpringBus;
    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;
    
    /**
     * WS服务配置。
     * 
     * @author swordshake
     * @since 1.0
     */
    @Configuration
    public class WebServiceConfig {
      @Bean(name = "cxfServlet")
      public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
      }
    
      @Bean(name = Bus.DEFAULT_BUS_ID)
      public SpringBus springBus() {
        return new SpringBus();
      }
    
    
    @Autowired
    private WSDemoService demoService; //由于在实现类中加了@Service因此此处无需初始化实例
        
      /* 注册服务示例*/
     @Bean
     public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus, demoService);
        endpoint.publish("/demoservice");
        return endpoint;
      }
    
    }
    
    

    4. 启动并访问http://localhost:8080/demo-web/ws/demoservice?wsdl

    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.demo.XX.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://impl.ws.demo.XX.com/" name="WSDemoServiceImplService" targetNamespace="http://ws.demo.XX.com">
    <wsdl:import location="http://localhost:8080/demo-web/ws/demoservice?wsdl=WSDemoService.wsdl" namespace="http://impl.ws.demo.XX.com/"> </wsdl:import>
    <wsdl:binding name="WSDemoServiceImplServiceSoapBinding" type="ns1:WSDemoService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="hello">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="hello">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="helloResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="WSDemoServiceImplService">
    <wsdl:port binding="tns:WSDemoServiceImplServiceSoapBinding" name="demoservicePort">
    <soap:address location="http://localhost:8080/demo-web/ws/demoservice"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    

    客户端调用

    1. 使用SoapUI验证调用。

    image.png

    2.IDEA 生成客户端代码

    右键生成的目标包 -> 菜单拉倒最后一个 “Web Services"

    注意:

    1.先拷贝出wsdl内容本地新建一个demo.wsdl文件;文件内容最上方必须加上

    "<?xml version="1.0" encoding="UTF-8"?>"否则提示“Wsdl url is not valid”。

    2.Web Service Platform 选择下图所示,其它几个选项IDEA没有集成对应的Jar包无法使用。

    imagefd13c.png

    测试代码如下:

     public static void main(String[] args) throws Exception {
        WSDemoServiceImplService service = new WSDemoServiceImplService(
            //实际生产环境需要根据wsdl的文件路径赋值URI,此处为本地测试写死的绝对路径;另wsdl中的访问地址要指向实际的调用环境
            WSDemoServiceImplService.WSDEMOSERVICEIMPLSERVICE_WSDL_LOCATION,
            WSDemoServiceImplService.WSDEMOSERVICEIMPLSERVICE_QNAME);
        System.out.println(service.getDemoservicePort().hello("haha"));
      };
    

    参考连接:

    [ 1 ] https://blog.csdn.net/lthaoshao/article/details/83020989

    相关文章

      网友评论

        本文标题:Spring Boot2.X 发布 WebService

        本文链接:https://www.haomeiwen.com/subject/lzeaoqtx.html