ServletRegistration url与Spring DispatcherServlet的映射冲突

时间:2013-02-22 16:19:02

标签: java servlets spring-mvc servlet-3.0

我跟着this example通过带有Spring的WebApplicationInitializer - >来配置我的DispatcherServlet。 javax.servlet.ServletContainerInitializer

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
    mvcContext.register(MyConfiguration.class);

    ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(mvcContext));
    appServlet.setLoadOnStartup(1);

    Set<String> mappingConflicts = appServlet.addMapping("/");
    if (!mappingConflicts.isEmpty()) {
        for (String s : mappingConflicts) {
            LOGGER.error("Servlet URL mapping conflict: {}", s);
        }
        throw new IllegalStateException("'appServlet' cannot be mapped to '/'");
    }
}   

当我启动Tomcat时,我得到上面的IllegalStateException因为apparently there is already a Servlet映射到/而我只能假设这是Tomcat的默认Servlet。如果我忽略mappingConflicts,我的DispatcherServlet不会映射到任何东西。

有没有办法用我自己覆盖这个默认的servlet映射,或者我将DispatcherServlet映射到/*

This answer通过更改您在Catalina webapps文件夹中部署应用程序的位置来提供解决方案,但我希望能够减少干扰。

1 个答案:

答案 0 :(得分:0)

事实证明,您可以通过Java在DispatcherServlet上映射/或任何其他Servlet(而不是您可以随时执行此操作的xml),但仅限于Tomcat版本&gt; 7.0.14,我在7.0.12。

请参阅this Bugzilla issue进行讨论。