1、错误信息
异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters
Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.iot.framework.core.response.CommResponse com.iot.basic.config.api.client.configdata.PushRuleClient.getByEventId(java.lang.String,java.lang.String) at feign.Util.checkState(Util.java:127) at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:117) at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:188) at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66) at feign.hystrix.HystrixDelegatingContract.parseAndValidatateMetadata(HystrixDelegatingContract.java:46) at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:154) at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:52) at feign.Feign$Builder.target(Feign.java:251) at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:55) at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:238) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:267) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:247) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ... 43 common frames omitted
2、错误原因
报错信息翻译过来就是:方法有太多的Body参数。根据错误提示信息,可以看到具体的是哪个方法,然后找到对应的方法,查看参数是否有误,当通过Feign客户端调用其他服务的接口时,请求参数可以有多个,用spring自带的注解用的方式不对,所以才导致报错。在同一个方法参数位置,@RequestParam注解可以用多个,但是@RequestBody注解只能用一个。
以下是我项目里用的方式,String devModel, String eventId这两个参数没有用@RequestParam注解说明,导致上述报错。
@ApiOperation("根据模型事件查询消息规则") @RequestMapping(value = "/pushRule/getByEventId",method = RequestMethod.GET) public CommResponse<List<PushRuleResponse>> getByEventId(String devModel, String eventId);
3、解决方案
解决方法其实很简单,在方法的参数上加上@RequestParam注解(同时可以用多个)即可。这里有两个参数,加上两个@RequestParam注解。如果有一个对象参数是要用@RequestBody注解的(这个注解同时只能用一个)。
@ApiOperation("根据模型事件查询推送规则") @RequestMapping(value = "/pushRule/getByEventId",method = RequestMethod.GET) public CommResponse<List<PushRuleResponse>> getByEventId(@RequestParam String devModel, @RequestParam String eventId);