【问题标题】:ServletRegistration url mapping conflict with Spring DispatcherServletServletRegistration url 映射与 Spring DispatcherServlet 冲突
【发布时间】:2013-02-08 07:57:48
【问题描述】:

我跟着this example用Spring的WebApplicationInitializer通过java配置了我的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 文件夹中的部署位置提供了一个解决方案,但我希望能够减少干扰。

【问题讨论】:

    标签: java servlets spring-mvc servlet-3.0


    【解决方案1】:

    事实证明,您可以通过 Java 映射 DispatcherServlet/ 上的任何其他 Servlet(与您始终可以执行此操作的 xml 相反),但仅限于 Tomcat 版本 > 7.0.14,我使用的是 7.0。 12.

    请参阅this Bugzilla issue 进行讨论。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2014-07-11
      • 2013-04-24
      • 2012-05-12
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多