Accessing multiple controllers with same request mapping

前端 未结 3 1691
野的像风
野的像风 2020-12-16 14:12

Please find my HomeController and DemoController

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

class DemoController{
@Reque         


        
相关标签:
3条回答
  • 2020-12-16 14:47

    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.

    0 讨论(0)
  • 2020-12-16 14:55

    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(){
       }
    }
    
    0 讨论(0)
  • 2020-12-16 14:59

    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

    0 讨论(0)
提交回复
热议问题