0

I have 2 GET handler methods

 @RestController
 public class TestController {

    ...

    @GetMapping(name = "/test")
    public Test testMethod() {
        return testService.getTest();
    }

    @GetMapping(name = "/test/{count}")
    public List<Test> getTestList2(@PathVariable(name = "count") Integer count) {

        return testService.getTestList(count);
    }
 }

And I get error:

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'testController' method 
public java.util.List<models.Test> TestController.getTestList2(java.lang.Integer)
to {[],methods=[GET]}: There is already 'testController' bean method

If I comment one method all work fine

1 Answer 1

1

What you are doing mistake is you are telling @GetMapping name rather that its value, It might feel both of them works the same way but it has tiny differences.

RequestMapping.name: Assign a name to this mapping.

and

RequestMapping.value: The primary mapping expressed by this annotation. Supported at the type level as well as at the method level! When used at the type level, all method-level mappings inherit this primary mapping, narrowing it for a specific handler method.

@RestController
public class TestController {

     @GetMapping(value = "/test")
     public String testMethod() {
        return "Hello from test";
     }

     @GetMapping(value = "/test/{count}")
     public String testMethod(@PathVariable(value = "count") Integer count) {
        return "Hello from Parameterized Test. Count: " + count;
     }
}

Hence you are specifying the path or route of your Controller it all always preferable to specify the value

Sign up to request clarification or add additional context in comments.

1 Comment

@ip696 you are not hitting the correct url. It should be localhost:8080/test/3 . Any way I rewrote the answer and tested myself. Read it again and try. It should fix your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.