nested exception is java.lang.IllegalStateException: Method has too many Body parameters问题解决

发布于:2023-01-12 ⋅ 阅读:(896) ⋅ 点赞:(0)

背景

增加了一个定制化的对外发布的接口,编译的时候没有任何问题,在启动的时候突然报了nested exception is java.lang.IllegalStateException: Method has too many Body parameters这个错误。

我看了下,发现是因为fegin里面调用的时候,需要在参数前增加@RequestBody

增加之后,启动OK了。

feign多参数

GET方式

错误写法

@GetMapping(value="/test")
public void Modeltest(finalString name,finalint age);

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

Causedby:java.lang.IllegalStateException:MethodhastoomanyBodyparameters:publicabstractcom.chhliu.springboot.restful.vo.Usercom.chhliu.springboot.restful.feignclient.UserFeignClient.findByUsername(java.lang.String,java.lang.String)

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

正确写法

@RequestMapping(value="/test", method=RequestMethod.GET) Model test(@RequestParam(“name”) final String name,@RequestParam(“age”) final int age);

POST方式

错误写法

publicint save(@RequestBodyfinal Person p,@RequestBodyfinal UserModel user);

feign中你可以有多个@RequestParam,但只能有不超过一个@RequestBody。

正确写法

public int save(@RequestBodyfinal Person p,@RequestParam(“userId”) String userId,@RequestParam(“userTel”) String userTel);


网站公告

今日签到

点亮在社区的每一天
去签到