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指定入参类型