Spring mvc default display handler

Basically, using Spring MVC, I am trying to create a router controller that will use any URL that has not yet been processed by another controller, and redirect it to the appropriate resource or redirect the search request if there is no resource to be found. Using @RequestMapping(value="/{qry}", method = RequestMethod.GET) was successful in capturing requests that were not already captured by my other controllers (this seems to work by first checking the most specific mappings), and then I Could do any shipment I needed. However, as soon as I put "/" in the request, the display breaks and returns 404.

In other words, "/some-long-path-or-something" correctly displays this catch-all controller, but "/some/other/path" (which does not map to any other controller) will not catch my catch-all.

How can this be implemented? I read a few things about the default hooks and handlers, but found no luck finding a solution.

Thanks for any suggestions!

+4
source share
2 answers

Out of the box, Spring automatically registers a DefaultAnnotationHandlerMapping bean that matches controller requests using annotations. Its default behavior is fine in most cases.

If you declare your own DefaultAnnotationHandlerMapping in the context, it will override the default value and allow you to set its defaultHandler property, which will be used when none of the explicit handlers match. Just insert the "caught" controller into this property, and Bob is your uncle.

+5
source

Why not just catch 404 and process accordingly? If I'm not mistaken, I think that you are trying to execute.

You can put this in your web.xml to handle 404:

 <error-page> <error-code>404</error-code> <location>/yourHandlerPage</location> </error-page> 

Did I miss something?

+3
source

Source: https://habr.com/ru/post/1339882/


All Articles