java—使用spring boot设置附加cxfservlet

2guxujil  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(360)

我目前正在尝试让一个cxfservlet与常规的webmvcservlet一起运行。我的cxf servlet定义了多个端点,即: /api/v1/* /soap/v2/* /internal-api/v1/* 我还想要 DispatcherServlet 让spring mvc在 /api/v2/* 配置时:

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

关于cxf的所有东西都能工作,但没有更多 @Controller 在SpringBoot中,应用程序不再是可访问的(当然,现在一切都指向cxfservlet)
但当我配置时:

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

只有使用这样的url,cxf的端点现在才可以访问 http://localhost:8080/api/v1/api/v1/test .
我该如何配置spring启动应用程序呢 /api/v2/* 是否指向springmvcservlet,而cxfservlet仍然像上面描述的那样工作?

6l7fqoea

6l7fqoea1#

手动注册dispatcher servlet,而不是让spring boot autoconfiguration这样做,解决了以下问题:

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

请注意,在/*以外的任何位置注册cxfservlet都会破坏cxfservlet的路由。

相关问题