Feign报错:nested exception is java.lang.IllegalStateException: Method has too many Body parameters...

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。

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值