Search code examples
spring-bootjax-rscxf

Spring Boot Apache CXF JAX-RS service context path / base URI


I configure my JAXRS Server in Spring Boot like so:

    JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
    factoryBean.setBus(this.bus);
    factoryBean.setFeatures(singletonList(swagger2Feature()));
    factoryBean.setServiceBeans(Arrays.asList(blah(), blah2(), blah3()));
    factoryBean.setAddress("/api/v1/"); // HERE

    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJaxbJsonProvider());
    factoryBean.setProviders(providers);

    BindingFactoryManager manager = factoryBean.getBus().getExtension(BindingFactoryManager.class);
    JAXRSBindingFactory restFactory = new JAXRSBindingFactory();
    restFactory.setBus(factoryBean.getBus());
    manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, restFactory);

    return factoryBean.create();

However, the URLs always require /services in front, which is a nuisance (but not the end of the world). Is there any way I can remove /services and just get it deployed to the root context?


Solution

  • If you have not created your own CxfServlet bean you can set the path by setting cxf.path property in your application.properties file

    cxf.path=/
    

    Another way is to override ServletRegistrationBean.

    @Bean
    public ServletRegistrationBean cxfServletRegistration() {
        String urlMapping = "/*";
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new CXFServlet(), urlMapping);
        registration.setLoadOnStartup(-1);
        return registration;
    }