Accessing multiple controllers with same request m

2020-02-28 00:21发布

问题:

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

回答1:

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



回答2:

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.



回答3:

Unfortunately, this is not possible. The request mapping has to be unique otherwise the application can't determine which method the incoming request should be mapped to.

What you can do instead is to extend the request mapping:

class HomeController{

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

class DemoController{

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