How to debug Spring MVC url mapping?

For problems like this I feel like the best "entrypoint" to start debugging is the method getHandler(HttpServletRequest request) of the DispatcherServlet.

Inside this method every configured HandlerMapping is inspected if it responsible for handling your specific request. If your request makes it this far, you can be quite sure that it is a configuration problem inside the spring context.

Watch out for handlers of the type RequestMappingHandlerMapping (or DefaultAnnotationHandlerMapping, if you are using an older version of Spring), these are normally the HandlerMapping used by annotation based Controller configuration.

In the other case, make sure there is nothing "in front" of the DispatcherServlet filtering your requests (like in your case)


Daniele Torino's post got me down the correct path by mentioning TRACE in addition to DEBUG. In addition to the logging framework, I think it matters which version of Spring you are using. In Spring 3, which we recently moved from, I think DEBUG was enough. However, in Spring 5, the actual mappings aren't listed. I recall that (in Spring 3.x) I used to see the mappings just by setting

<logger name="org.springframework.web" level="DEBUG" />

, but now (in Spring 5.x) that only lists the number of mappings.

DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - 14 mappings in 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'

In Spring 5, the actual mappings weren't logged until I added

<logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="TRACE" /> 

to the log.properties file. (I recommend using TRACE sparingly.) With that in place, the log output includes several lines like:

TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped 1 handler method(s) for class com.yourcompany.YourClass: {public org.springframework.web.servlet.ModelAndView com.yourcompany.YourClass.someMethod(<your parameters and models here>,javax.servlet.http.HttpServletRequest)={[/your-request-mapping-url],methods=[GET]}}