我跟着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文件夾中的部署位置提供了一個解決方案,但我希望能夠減少侵入性。