2017-11-09 1 views
0

내가 처리기 메서드스프링 MVC RestController 모호한 PathVariable 매핑

@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); 
    } 
} 

GET 2 을 그리고 오류 얻을 : 나는 하나의 방법을 언급하는 경우

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 

이 모든 일을 잘

답변

-1

당신 당신은 그 값이 아니라 그 이름을 @GetMapping에게 말하고있는 것이 실수인지, 그들 모두가 같은 방식으로 작동한다고 느낄지 모르지만 그것은 가지고 있습니다. 작은 차이.

RequestMapping.name: Assign a name to this mapping.

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; 
     } 
} 

은 따라서 당신은 당신의 컨트롤러의 경로 또는 경로를 지정하는 그것을 value

+0

HTTP 지정 항상 바람직 모든 : // localhost를 : 8080/테스트를? count = 3 not worked – ip696

+0

@ ip696 올바른 URL을 치지 않고 있습니다. \t ** localhost : 8080/test/3 **이어야합니다. 어쨌든 나는 답을 다시 썼다. 그것을 다시 읽고 시도하십시오. 그것은 당신의 문제를 해결해야합니다. –

+0

@ ip696 이것은 아무런 반응이 없으면 어리 석다. 당신은 도움을 구하고 공동체 또한 원합니다. 다시 읽어 보시고 도움이된다고 생각하시면 도움을 받으십시오. –