Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc default mapping handler

Basically, using Spring MVC, I'm trying to create a router controller that will take any URL that hasn't already been handled by another controller and route it to its respective resource or forward a search request if no resource could be found. Using @RequestMapping(value="/{qry}", method = RequestMethod.GET) was successful in grabbing requests that weren't grabbed already by my other controllers (this seems to work by checking the most specific mappings first) and then I could do whatever forwarding I needed. However, as soon as I put a "/" in the request, the mapping breaks and returns a 404.

So in other words, "/some-long-path-or-something" maps correctly to this catch-all controller, but "/some/other/path" (which does not map to any other controller) is not caught by my catch-all.

How can this be implemented? I've read a few things about interceptors and default handlers, but with no luck in finding a solution.

Thanks for any suggestions!

like image 259
jerluc Avatar asked Feb 25 '23 02:02

jerluc


1 Answers

Out-of-the-box, Spring automatically registers a DefaultAnnotationHandlerMapping bean, which matches requests to controllers using the annotations. Its default behaviour is fine in most situations.

If you declare your own DefaultAnnotationHandlerMapping in the context, then it will override the default one, and will allow you to set its defaultHandler property, which will be used when none of the explicit handlers match. Just inject the "catch-all" controller into that property, and Bob's Your Uncle.

like image 55
skaffman Avatar answered Mar 09 '23 01:03

skaffman