1

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.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

0

So turns out you can map DispatcherServlet or any other Servlet on / through Java (as opposed to xml where you could always do this), but only on Tomcat versions > 7.0.14, I was on 7.0.12.

See this Bugzilla issue for discussion.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724