我跟着this example通过Java Spring的WebApplicationInitializer
来配置我的DispatcherServlet - >javax.servlet.ServletContainerInitializer
:ServletRegistration URL映射冲突
@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。如果我忽略映射冲突,我的DispatcherServlet
未映射到任何内容。
有没有什么办法覆盖这个默认的servlet映射与我自己或我坚持映射我的DispatcherServlet
到/*
?
This answer通过更改您的应用程序在Catalina webapps文件夹中的部署位置提供了一个解决方案,但我希望能够减少侵入性。