@FeignClient接口上不允许使用@RequestMapping注解的问题及解决方法咨询
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 ); }
2. Use the path attribute in @FeignClient (Recommended)
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 formapi/products/{id} - If
path = "api/products/"(with trailing slash), using@GetMapping("{id}")(no leading slash) will also formapi/products/{id}
Stick to one style to keep your code consistent.
内容的提问来源于stack exchange,提问作者DiegoMG

