SpringBoot @RestController, Ambiguous mapping found
Asked Answered
O

3

14

Hi I have a simple RestController in my Sample:

@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 SampleController for start 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 occur:

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 the problem can be? There can not be more RequestMappings in one RestController?

Very thanks for reply

Overpowering answered 9/3, 2015 at 12:38 Comment(0)
P
32

You have to use value attribute to define the mapping. You've used name right now, which just provides a name to the mapping, but doesn't define any mapping at all. So currently both your methods are unmapped (in which case, both are mapped to 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);
}
Psychopathology answered 9/3, 2015 at 13:6 Comment(0)
D
2

Or You Can use,

@GetMapping("/getName")

It is the same usage of method with value,it is new version of specifying method ="POST" with request mapping value.

Diagnosis answered 3/10, 2017 at 10:33 Comment(0)
T
0

In RequestMapping(value="/name") always use value for path not name. You can use method wise also like GETMapping("/getname") PostMapping("/addname")

Trabeated answered 6/9, 2019 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.