Spring Boot Apache CXF JAX-RS服务上下文路径/基URI

时间:2017-07-11 15:26:13

标签: spring-boot jax-rs cxf

我在Spring Boot中配置我的JAXRS服务器如下:

    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();

但是,网址始终需要/services,这是一件令人讨厌的事情(但不是世界末日)。有什么方法可以删除/services并将其部署到根上下文中吗?

1 个答案:

答案 0 :(得分:4)

如果您尚未创建自己的CxfServlet bean,可以通过在application.properties文件中设置cxf.path属性来设置路径

cxf.path=/

另一种方法是覆盖ServletRegistrationBean

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