簡體   English   中英

Spring MVC RestController Ambiguous PathVariable 映射

[英]Spring MVC RestController Ambiguous PathVariable mapping

我有 2 個GET處理程序方法

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

我得到錯誤:

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

如果我評論一種方法一切正常

你在做的錯誤是你告訴@GetMapping名稱而不是它的值,它可能覺得它們的工作方式相同,但它們有微小的差異。

RequestMapping.name :為此映射分配一個名稱。

RequestMapping.value :這個注解表達的主要映射。 在類型級別和方法級別都支持! 當在類型級別使用時,所有方法級別的映射都會繼承這個主要映射,將它縮小到特定的處理程序方法

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM