SpringBoot @RestController, ambiguous mapping detected

Hi, I have a simple RestController in my example:

@RestController public class PersonController { @RequestMapping(name = "/getName", method = GET) public String getName() { return "MyName"; } @RequestMapping(name = "/getNumber", method = GET) public Double getNumber(){ return new Double(0.0); } } 

And I have a SampleController to run SpringBoot:

 @SpringBootApplication @Controller public class SampleController { public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } } 

When I try to run SampleCotroller, the following exception is thrown:

 Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method public java.lang.Double com.web.communication.PersonController.getNumber() to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personController' bean method public java.lang.String com.web.communication.PersonController.getName() mapped. 

Where is the problem? Cannot be more RequestMappings in one RestController?

Thank you very much for your reply.

+8
source share
3 answers

You must use the value attribute to define the mapping. You used name right now, which just provides a name for matching, but does not define any mapping at all. Therefore, at present, both of your methods are not displayed (in this case, both are displayed on the same path). Change the methods to:

 @RequestMapping(value = "/getName", method = GET) public String getName() { return "MyName"; } @RequestMapping(value = "/getNumber", method = GET) public Double getNumber(){ return new Double(0.0); } 
+27
source

Or you can use,

 @GetMapping("/getName") 

This is the same use of a method with a value; this is a new version of the specifying method = "POST" with the request display value.

+2
source

In RequestMapping (value = "/ name"), always use the value for the path, not the name. You can use the wise method as well as GETMapping ("/ GetName") PostMapping ("/ addname")

0
source

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


All Articles