I followed this example to configure my DispatcherServlet through java with Spring's WebApplicationInitializer --> 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 '/'");
}
}
When I start-up Tomcat, I get the above IllegalStateException because apparently there is already a Servlet mapped to / and I can only assume that is Tomcat's default Servlet. If I ignore the mappingConflicts, my DispatcherServlet isn't mapped to anything.
Is there any way to override this default servlet mapping with my own or am I stuck mapping my DispatcherServlet to /*?
This answer provides a solution by changing where your application is deployed in the Catalina webapps folder, but I was hoping for something less intrusive.