Multiple endpoints using CXF and Spring Boot

I recently ran into a problem while porting a couple of old SOAP services that currently run on JBoss into a Spring Boot application. As a start https://blog.codecentric.de/en/2016/02/spring-boot-apache-cxf/  provided a good introduction on how use CXF and Spring Boot, but the example only shows one endpoint.

For some reason I couldn’t find any good resources on how to create two independent endpoints using CXF and Spring Boot so I’m writing it down in case somebody else stumbles into the same problem.

The solution is quite simple: In order to get multiple independent endpoints it is important to create one SpringBus and ServletRegistrationBean for each base URL.

As an example, say you want to have two versions of an API:

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

Using Spring Boot we can define the two following configuration classes.

@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;
    }
}
@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;
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s