记录今天遇到的feign多参数问题
1.Post方式
错误写法示例如下:
public int save(@RequestBody final User u, @RequestBody final School s);
错误原因:
fegin中可以有多个@RequestParam,但只能有不超过一个@RequestBody,@RequestBody用来修饰对象,但是既有@RequestBody也有@RequestParam,
那么参数就要放在请求的Url中,@RequestBody修饰的就要放在提交对象中。
注意!!! 用来处理@RequestBody Content-Type 为 application/json,application/xml编码的内容
正确写法示例如下:
public int save(@RequestBody final Person p,@RequestParam("userId") String userId,@RequestParam("userTel") String userTel);
2.Get方式
错误写法示例如下:
@RequestMapping(value="/test", method=RequestMethod.GET)
Model test(final String name, final int age);
错误原因:
异常原因:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰
,@RequestParam可以修饰多个,@RequestParam是用来修饰参数,不能用来修饰整个对象。
注意:@RequestParam Content-Type 为 application/x-www-form-urlencoded 而这种是默认的
正确写法示例如下:
@GetMapping("/getSchoolDetail")
public ResultMap getSchoolDetail(@RequestParam("kSchoolId") LongkSchoolId,
@RequestParam("kSchoolYearId") Long kSchoolYearId);