Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Controller to handle all requests not matched by other Controllers

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?

like image 940
rurounisuikoden Avatar asked Apr 13 '16 09:04

rurounisuikoden


People also ask

How does Spring controller handle multiple request?

Spring manages different scopes (prototype, request, session, singleton). If two simultaneous requests access a singleton bean, then the bean must be stateless (or at least synchronized to avoid problems). One way you can handle multiple requests is to have multiple physical servers.

Can we have multiple controllers in the spring application?

We may construct numerous controllers at once in Spring MVC. Each controller class must be annotated with the @Controller annotation.

What is difference between controller and rest controller in spring?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.

Can we have multiple rest controllers in spring boot?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.


2 Answers

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";
}
like image 188
Rizwan M.Tuman Avatar answered Oct 11 '22 01:10

Rizwan M.Tuman


@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.

like image 34
patrykos91 Avatar answered Oct 11 '22 02:10

patrykos91