Access to multiple controllers with the same query mapping

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 an index request, which one will be executed? I wanted to know how we can have the same query matching value for multiple controllers.

+5
source share
3 answers

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

You can’t even use parameters to distinguish endpoints because they are not evaluated when looking for a suitable handler (applicable to Servlet environments). From @RequestMapping javadoc:

In the Servlet environment, parameter mappings are considered constraints that are applied at the type level. The primary mapping of the path (i.e., the specified URI value) should still uniquely identify the target handler, while parameter mappings simply express the prerequisites for calling the handler.

Note that you can do the opposite, so multiple URLs can point to the same handler. Check out Spring MVC: Matching Multiple URLs to the Same Controller

+2
source

fooobar.com/questions/1239765 / ... is only partially correct at the moment.

You can have multiple controller methods use the same URI if you provide Spring with enough additional information about which one should be used. Do you need to do this is another question. I would certainly not recommend using the same URI in two separate controller classes in order to avoid confusion.

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 complete documentation of what is possible with URI overloading, you should refer to the @ReqeustMapping documentation: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ RequestMapping.html . And in particular, https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- for section request parameters.

+5
source

Unfortunately this is not possible. The display of the request must be unique, otherwise the application cannot determine to which method the incoming request should be bound.

Instead, you can expand the display of the query:

 class HomeController{ @RequestMapping(value="home/index") public void home(){ } } class DemoController{ @RequestMapping(value="demo/index") public void demo(){ } } 
+4
source

Source: https://habr.com/ru/post/1239765/


All Articles