July July - 1 year ago 123
Java Question

Spring Boot REST mapping conflict event in different controller

I have two Rest controllers in my Spring boot(1.4.0 RELEASE) app:

@RestController("/ctrl")
public class TestController {
@GetMapping
public void test() {

}
}


@RestController("/ctrl2")
public class TestController2 {
@GetMapping
public void test() {

}
}


When I run the Spring boot app:

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/ctrl2' method
public void com.xxx.controller.TestController2.test()
to {[],methods=[GET]}: There is already '/ctrl' bean method
public void com.xxx.controller.TestController.test() mapped.


If I delete one controller, everything will be fine and the app can start well. So where is the problem?

Answer Source

All you have to do is change your code like so:

@RestController
@RequestMapping("/ctrl")
public class TestController {
    @GetMapping
    public void test() {

    }
}


@RestController
@RequestMapping("/ctrl2")
public class TestController2 {
    @GetMapping
    public void test() {

    }
}

Notice that I have added the RequestMapping annotation to the controllers.

The name you where using in the @RestController annotation does not have to do with mapping. It's used as the name of the bean that will be registered in the Spring context