I have a series of Controllers with Request Mappings that match certain URL's. I also want a Controller that will match any other URL not matched by the other Controllers. Is there a way to do this in Spring MVC? For example, could i have a controller with @RequestMapping(value="**") and change the order in which Spring Controllers are processed so this Controller is processed last to catch all unmatched requests? Or is there another way to achieve this behaviour?
Asked
Active
Viewed 2.4k times
15
-
If I understand correctly, you want to handle all 404s? But possibly do something useful, rather than return 'not found'? – Leon Apr 13 '16 at 09:50
-
2Yes your idea would work. Have you tried it? See https://github.com/stormpath/spring-mvc-rest-exhandler/blob/master/example/src/main/java/com/stormpath/blog/spring/mvc/rest/exhandler/DefaultController.java for example – reto Apr 13 '16 at 09:52
-
Yes, i want a way to process URL's not matched by any other Controller i have made. – rurounisuikoden Apr 13 '16 at 09:55
-
Wouldn't the default controller you have provided in that link be matched before other requestMappings? – rurounisuikoden Apr 13 '16 at 09:56
-
https://stackoverflow.com/questions/7276989/how-to-set-the-context-path-of-a-web-application-in-tomcat-7-0/51167549#51167549 – Kanagavelu Sugumar Jul 16 '18 at 12:33
3 Answers
16
If your base url is like that= http://localhost/myapp/ where myapp is your context then myapp/a.html, myapp/b.html myapp/c.html will get mapped to the first 3 method in the following controller. But anything else will reach the last method which matches **. Please note that , if you put ** mapped method at the top of your controller then all request will reach this method.
Then this controller servrs your requirement:
@Controller
@RequestMapping("/")
public class ImportController{
@RequestMapping(value = "a.html", method = RequestMethod.GET)
public ModelAndView getA(HttpServletRequest req) {
ModelAndView mv;
mv = new ModelAndView("a");
return mv;
}
@RequestMapping(value = "b.html", method = RequestMethod.GET)
public ModelAndView getB(HttpServletRequest req) {
ModelAndView mv;
mv = new ModelAndView("b");
return mv;
}
@RequestMapping(value = "c.html", method = RequestMethod.GET)
public ModelAndView getC(HttpServletRequest req) {
ModelAndView mv;
mv = new ModelAndView("c");
return mv;
}
@RequestMapping(value="**",method = RequestMethod.GET)
public String getAnythingelse(){
return "redirect:/404.html";
}

Mustofa Rizwan
- 10,215
- 2
- 28
- 43
-
After this configuration my swagger stopped working. https://github.com/springfox/springfox/issues/2532 – Kanagavelu Sugumar Jul 04 '18 at 05:40
-
use @RequestMapping("/anything/") instead of @RequestMapping("/") for this configuraration – Mustofa Rizwan Jul 04 '18 at 05:44
-
But that will handle only non mapped requests under `/anything` – Kanagavelu Sugumar Jul 04 '18 at 05:48
-
then remove the last method from this class value="**", and use custom exception handler... which will make sure that any non mapped request goes to a certain 404 page – Mustofa Rizwan Jul 04 '18 at 05:49
-
Exception handler wont solve my problem, want to handle it inside @RequestMapping method. Anyhow Thanks & plus1. – Kanagavelu Sugumar Jul 04 '18 at 05:55
14
@RequestMapping (value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<String> defaultPath() {
LOGGER.info("Unmapped request handling!");
return new ResponseEntity<String>("Unmapped request", HttpStatus.OK);
}
This will do the work with proper order of controller matching. It will be used when nothing is matched.

patrykos91
- 3,506
- 2
- 24
- 30
-
7This is problematic. For some reason, this controller method catches request to static resources too. – manash Jan 15 '19 at 16:25
-1
You should use "*" as the value but inside the brackets
@RequestMapping(value={"*"}, method={RequestMethod.GET, RequestMethod.POST})
public String allRequests() {
return "index.html";
}