Spring Boot & Apache CXF 开发具有多Endpoint的 WebService

通过Spring Boot 整合Apache CXF开发WebService,可以避免以前Spring大量的xml配置文件。采用这种方式开发 WebService的公开例子不少,例如:

详解Spring boot+CXF开发WebService Demo

Spring Boot & Apache CXF——简单的webservice,并实现用户验证

Spring boot 整合CXF开发web service

spring boot整合cxf发布webservice服务和cxf客户端调用

Spring Boot & Apache CXF – How to SOAP in 2016

上述用例简单直观,能很快地实现单个Endpint的WebService服务端,本文不再赘述。但实际情况中,往往是在一个Web服务中具有多类业务的Endpoints,甚至存在相同业务但不同版本的Endpoints。这时应该怎么实现?

实际上,只要在CXF Configuration文件继续添加Endpoint 和相应的WebService实现即可。如下是目前已验证的实例代码。

@Configuration
public class CxfConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myService());
        endpoint.publish("/myservice");
        return endpoint;
    }

    /**
     * Another Webservice of Mine.
     * @return
     */
    @Bean
    public AnotherService anotherService() {
        return new AnotherServiceImpl();
    }

    @Bean
    public EndpointImpl anotherEndpoint(AnotherService anotherService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), anotherService);
        endpoint.publish("/another");
        return endpoint;
    }
}

如果希望存在多个版本的Endpoint,可通过创建不同版本的 CxfConfig 类来实现。每个CxfConfig类中都包含一个SpringBus和 ServletRegistrationBean。

例如,对于如下两套版本的API:

/api/v1/orders/get
/api/v1/orders/create
/api/v2/orders/get
/api/v2/orders/create

可以通过SpringBoot定义如下两个Configuration 类:
SomeApiV1Config 类:

@Configuration
public class SomeApiV1Config {

    @Bean
    public SpringBus springBusV1() {
        SpringBus bus = new SpringBus();
        bus.setId("v1");
        return bus;
    }

    @Bean
    public ServletRegistrationBean v1Servlet() {
        CXFServlet cxfServlet = new CXFServlet();
        cxfServlet.setBus(springBusV1());

        ServletRegistrationBean servletBean = new ServletRegistrationBean(cxfServlet, "/api/v1/*");
        servletBean.setName("v1");
        return servletBean;
    }

    @Bean
    public EndpointImpl getOrderV1(GetOrderServiceV1 service) {
        EndpointImpl endpoint = new EndpointImpl(springBusV1(), service);
        endpoint.publish("/orders/get");
        return endpoint;
    }

    @Bean
    public EndpointImpl createOrderV1(CreateOrderServiceV1 service) {
        EndpointImpl endpoint = new EndpointImpl(springBusV1(), service);
        endpoint.publish("/orders/create");
        return endpoint;
    }
}

SomeApiV2Config 类:

@Configuration
public class SomeApiV2Config {

    @Bean
    public SpringBus springBusV2() {
        SpringBus bus = new SpringBus();
        bus.setId("v2");
        return bus;
    }

    @Bean
    public ServletRegistrationBean v2Servlet() {
        CXFServlet cxfServlet = new CXFServlet();
        cxfServlet.setBus(springBusV2());

        ServletRegistrationBean servletBean = new ServletRegistrationBean(cxfServlet, "/api/v2/*");
        servletBean.setName("v2");
        return servletBean;
    }

    @Bean
    public EndpointImpl getOrderV2(GetOrderServiceV2 service) {
        EndpointImpl endpoint = new EndpointImpl(springBusV2(), service);
        endpoint.publish("/orders/get");
        return endpoint;
    }

    @Bean
    public EndpointImpl createOrderV2(CreateOrderServiceV2 service) {
        EndpointImpl endpoint = new EndpointImpl(springBusV2(), service);
        endpoint.publish("/orders/create");
        return endpoint;
    }

}

上述示例,来源于Multiple endpoints using CXF and Spring Boot 一文。

你可能感兴趣的:(Java,WebService,spring,微服务)