Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing multiple controllers with same request mapping

Please find my HomeController and DemoController

class HomeController{
@RequestMapping(value="index")
public void home(){
}
}

class DemoController{
@RequestMapping(value="index")
public void demo(){
}
}

when I try to send a request to index, which one will get executed? I wanted to know how can we have same request mapping value for multiple controllers

like image 554
Monika Avatar asked Jan 04 '16 08:01

Monika


People also ask

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. A Spring MVC example with numerous controllers can be found here.

Can you have multiple rest controllers?

So what happens when you have two rest controller defined onto the same path? If you don't have any overlapping request mappings other than the code being slightly confusing, nothing will actually go wrong and you can successfully send requests to the methods inside each controller.

What is the difference between GetMapping and request mapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.


2 Answers

https://stackoverflow.com/a/34590355/2682499 is only partially correct at this point.

You can have multiple controller methods use the same URI so long as you provide Spring enough additional information on which one it should use. Whether or not you should do this is a different question. I would certainly not recommend using the same URI in two separate controller classes to avoid confusion, though.

You can do something like this:

class HomeController{
    @RequestMapping(value="/index", params = {"!name", "!foo"})
    public List<Something> listItems(){
        // retrieve Something list
    }

    @RequestMapping(value="/index", params = "name")
    public List<Something> listItems(String name) {
        // retrieve Something list WHERE name LIKE %name%
    }

    @RequestMapping(value="/index", params = {"!name", "foo"})
    public List<Something> listItems(String foo) {
        // Do something completely different
    }
}

For the full documentation on what is possible when overloading URIs you should reference the @ReqeustMapping documentation: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html. And, specifically https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- for the section request parameters.

like image 155
RuntimeBlairror Avatar answered Sep 21 '22 12:09

RuntimeBlairror


In Spring Web MVC this is not possible. Each mapping must be unique in your context. If not, you will receive a RuntimeException during context initialization.

You cannot even use parameters to differentiate your endpoints because they are not evaluated while searching for a suitable handler (applicable for Servlet environments). From @RequestMapping javadoc:

In a Servlet environment, parameter mappings are considered as restrictions that are enforced at the type level. The primary path mapping (i.e. the specified URI value) still has to uniquely identify the target handler, with parameter mappings simply expressing preconditions for invoking the handler.

Note that you can do the opposite, so multiple URLs can point to the same handler. Have a look at Spring MVC: Mapping Multiple URLs to Same Controller

like image 45
Marco Ferrari Avatar answered Sep 21 '22 12:09

Marco Ferrari