2017-11-09 24 views
0

私はハンドラメソッドSpring 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 を持っていると私はエラーを取得:私は1つの方法をコメントする場合

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これは何の反応もないことは不条理です。あなたは助けを求め、コミュニティも望んでいます。あなたがそれを見つけることができればそれはrecickkし、accpetedにしてください。 –

関連する問題