openFeign接口方法传递多个参数时错误:nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract xxx
这是因为feign客户端的方法参数没有用相关的注解

@RequestParam

  1. 客户端@RequestParam注解的value属性必须指定值,要和服务端接口参数名保持一致
  2. 如果需要传递多个字符串参数,要使用多个@RequestParam注解与服务端接口参数保持对应

@RequestBody

  1. @RequestBody 注解在服务端和客户端都需要使用
  2. 参数名和参数类型在服务端和客户端需要保持一致
  3. 一般用于传递对象

例:

远程Controller

@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @RequestMapping("/decrease")
    public CommonResult decrease(Long productId,Integer count){
        storageService.decrease(productId,count);
        return new CommonResult(200,"扣减库存成功");
    }
}

FeignClient

@FeignClient("seata-storage")
@Component
public interface StorageService {

    @PostMapping("/storage/decrease")
    CommonResult decrease(@RequestParam(value = "productId") Long productId, 
    					  @RequestParam(value = "count") Integer count);
}

注:
openFeign默认使用@RequestBody,但只允许一个@RequestBody,所以:

  1. 只传递一个参数无需注解
  2. 传递多个参数时要用@RequestParam

若远程Controlle方法参数有@PathVariable注解,则FeignClient传递参数也必须用@PathVariable注解,且指定@PathVariable的value对应Controller。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐