javaspringspring-bootcxf

Setting up an additonal CXFServlet with Spring Boot


I am currently trying to get a CXF Servlet to run along side the regular Web MVC Servlet. My CXF Servlet defines multiple endpoints namely:

I also want the DispatcherServlet for Spring MVC to run under /api/v2/*

When I configure:

 @Bean
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        final ServletRegistrationBean<CXFServlet> cxfServletServletRegistrationBean = new ServletRegistrationBean<>(new CXFServlet(), "/*");
        return cxfServletServletRegistrationBean;
    }

everything about CXF works, but no more @Controller in the spring boot app are reachable anymore.(well of course now everything is directed to the cxfservlet)

But when I configure:

 @Bean
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        final ServletRegistrationBean<CXFServlet> cxfServletServletRegistrationBean = new ServletRegistrationBean<>(new CXFServlet(), "/api/v1/*");
        return cxfServletServletRegistrationBean;
    }

the endpoints of cxf are only reachable now if I use an url such as this http://localhost:8080/api/v1/api/v1/test.

How can I configure the spring boot app, so /api/v2/* is directed to the spring mvc servlet and the cxf servlet still works like described above?


Solution

  • Registering a dispatcher servlet manually instead of letting spring boot autoconfiguration do it fixed the issue:

    @Configuration
    public class ServletConfig {
    
        @Bean
        public ServletRegistrationBean<CXFServlet> cxfServlet() {
            return new ServletRegistrationBean<>(new CXFServlet(), "/*");
        }
    
        @Bean
        public DispatcherServlet dispatcherServlet() {
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            dispatcherServlet.setThreadContextInheritable(true);
            dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
            return dispatcherServlet;
        }
    
        @Bean
        public DispatcherServletRegistrationBean dispatcherServletRegistration() {
            DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
                    dispatcherServlet(),
                    "/api/v2/*"
            );
            registration.setLoadOnStartup(0);
            registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
            return registration;
        }
    }
    

    Note that registering the cxf servlet under anything but /* will ruin the routes of the cxf servlet.