You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@FeignClient接口上不允许使用@RequestMapping注解的问题及解决方法咨询

Solution for @RequestMapping on @FeignClient Interface

Got it, let's tackle this FeignClient issue you're facing. Starting from Spring Cloud 2020.0.0 (the Ilford release), putting @RequestMapping directly on the @FeignClient interface is no longer supported—this was done to avoid path matching ambiguities that often cropped up when mixing Feign client paths with Spring MVC controller routes.

Here are two clean, official solutions to fix your code:

1. Move the base path to each method's request mapping

You can simply append the base path (api/products/) to each method's @GetMapping annotation. This makes each method's full path explicit and avoids the deprecated class-level @RequestMapping.

Modified code:

@FeignClient(name = "product-service")
public interface ProductClient {
    @GetMapping("api/products/{id}")
    ResponseEntity<Product> getProduct(@PathVariable("id") Long id);

    @GetMapping("api/products/{id}/stock")
    ResponseEntity<Product> updateStockProduct(
        @PathVariable("id") Long id,
        @RequestParam(name = "quantity", required = true) Integer quantity
    );
}

If you want to keep a unified base path for all methods in the client, use the built-in path parameter of @FeignClient. This is the official replacement for class-level @RequestMapping and keeps your code clean.

Modified code (this matches your original logic exactly):

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

Quick Note on Path Slashes

Be mindful of leading/trailing slashes to avoid duplicate or missing path segments. For example:

  • If path = "api/products" (no trailing slash), your method can use @GetMapping("/{id}") to form api/products/{id}
  • If path = "api/products/" (with trailing slash), using @GetMapping("{id}") (no leading slash) will also form api/products/{id}
    Stick to one style to keep your code consistent.

内容的提问来源于stack exchange,提问作者DiegoMG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:37:47