Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Spring MVC url mapping?

I am using Spring MVC 3 and am having an issue with a URL mapping. I have a method

 @Controller  public class DocumentController {       @RequestMapping( value="/docs/pupil/classes/{courseCategoryType}", method=RequestMethod.GET )       public ModelAndView listClassesForPupil( @PathVariable("courseCategoryType") final String courseCategoryType ){             System.err.print( "\n\n\n\t\t--- XXXXX ---\n\n\n" );       }  } 

I am trying to use the Spring URI template syntax, and I know that is getting mapped because in console I see:

 11:22:12,108  INFO DefaultAnnotationHandlerMapping:411 - Mapped URL path [/docs/pupil/classes/{courseCategoryType}] onto handler 'documentController'  11:22:12,108  INFO DefaultAnnotationHandlerMapping:411 - Mapped URL path [/docs/pupil/classes/{courseCategoryType}.*] onto handler 'documentController'  11:22:12,108  INFO DefaultAnnotationHandlerMapping:411 - Mapped URL path [/docs/pupil/classes/{courseCategoryType}/] onto handler 'documentController' 

However, when I enter the URLhttps://localhost/docs/pupil/classes/ACADEMIC in browser I get a 404 error and I do not see anything printed out in the console. I replaced the print out code that just throws an exception, and it didn't seem to get thrown either. A coworker suggested there should be a way of viewing how the URL resolution is being done but a Google search didn't seem to turn up anything.

Any suggestions as how to debug this?

like image 747
Sled Avatar asked Mar 28 '11 15:03

Sled


People also ask

How do I debug a Spring MVC project?

The main difference is you may not be running a java application but rather a web app. To start TC-server click the 'Start the server in debug mode' button. It looks like a bug and is right next to the green play button in the servers tab in the lower left corner of the screen.

How do I resolve no mapping?

Make sure you create your package and classes in src/main/java NOT in src/main/resources. If maven doesn't create the src/main/java folder, just create the folder and move the package folders/classes there. Doing this resolved the no mapping error for me. Hope this information will be useful to someone.

How does Spring MVC handle 404 error?

The 404 error code is configured properly, but it will caused the “. htm” extension handling conflict between the “servlet container” and Spring's “DispatcherServlet“. To solve it, try change the 404. htm to other file extension, for example 404.

How can an HTTP 404 status code be returned from a Spring MVC controller?

Spring MVC is a traditional application built using the Front Controller Pattern. DispatcherServlet, which acts as the front controller, is responsible for routing and request processing. As with any web application or website, Spring MVC returns the HTTP 404 response code when the requested resource can't be found.


2 Answers

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)

like image 83
Patrick Avatar answered Oct 01 '22 13:10

Patrick


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]}} 
like image 33
Capricorn1 Avatar answered Oct 01 '22 12:10

Capricorn1