Method has too many Body parameters的处理办法

SpringCloud Feign报错:Method has too many Body parameters

feign多参数问题

GET方式

错误写法


	//consumes=application/x-www-form-urlencoded
    @GetMapping(value = "/attach/base64", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String getBase64ByAttachId(Long attachId);

启动服务的时候,会报如下异常:

 
Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.chhliu.springboot.restful.vo.User com.chhliu.springboot.restful.feignclient.UserFeignClient.findByUsername(java.lang.String,java.lang.String)  

异常原因:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰 ,@RequestParam可以修饰多个,@RequestParam是用来修饰参数,不能用来修饰整个对象。

正确写法

	//consumes=application/x-www-form-urlencoded
    @GetMapping(value = "/attach/base64", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String getBase64ByAttachId(@RequestParam("attachId") Long attachId);

POST方式

错误写法

public int save(@RequestBody User u, @RequestBody School s);

异常原因feign中你可以有多个@RequestParam,但只能有不超过一个@RequestBody,@RequestBody用来修饰对象,但是既有@RequestBody也有@RequestParam,那么参数就要放在请求的url中,@RequestBody修饰的就要放在提交对象中。

注意 用来处理@RequestBody Content-Type 为 application/json、application/xml编码的内容

正确写法1

@PostMapping(value = "/save")
public int save(@RequestBody  Person p,@RequestParam("userId") String userId,@RequestParam("userTel") String userTel);

正确写法2

//consumes=application/json
@PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
public int save(Person p,@RequestParam("userId") String userId,@RequestParam("userTel") String userTel);

官方推荐使用consumes指定入参类型

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值