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?
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.
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.
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.
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.
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]}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With