@FeignClient 接口上不允许使用 @RequestMapping 注解

Die*_*oMG 16 spring-boot request-mapping spring-cloud-feign

我遇到了麻烦!正如我一直在阅读的那样,这种情况的发生是因为它不再被接受。但我该如何解决呢?这是我一直在尝试映射的代码。

@FeignClient(name = "product-service")
@RequestMapping("api/products/")
public interface ProductClient {

    @GetMapping("/{id}")
    ResponseEntity<Product> getProduct(@PathVariable("id") Long id);

    @GetMapping("/{id}/stock")
    ResponseEntity<Product> updateStockProduct(@PathVariable("id") Long id,@RequestParam(name = "quantity", required = true) Integer quantity);
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您的任何建议或解决方案!

Mad*_*hat 21

您可以使用path属性@FeignClient来定义要添加到所有 API 的路径前缀。文档在这里。

所以你的 Feign 客户端界面看起来像

@FeignClient(name = "product-service", path = "/api/products")
public interface ProductClient {

    @GetMapping("/{id}")
    ResponseEntity<Product> getProduct(@PathVariable("id") Long id);

    @GetMapping("/{id}/stock")
    ResponseEntity<Product> updateStockProduct(@PathVariable("id") Long id,@RequestParam(name = "quantity", required = true) Integer quantity);
}
Run Code Online (Sandbox Code Playgroud)