当前位置:   article > 正文

【Java异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters

caused by: java.lang.illegalstateexception: method has too many body paramet

1、错误信息

异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters

  1. Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.iot.framework.core.response.CommResponse
  2. com.iot.basic.config.api.client.configdata.PushRuleClient.getByEventId(java.lang.String,java.lang.String)
  3. at feign.Util.checkState(Util.java:127)
  4. at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:117)
  5. at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:188)
  6. at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66)
  7. at feign.hystrix.HystrixDelegatingContract.parseAndValidatateMetadata(HystrixDelegatingContract.java:46)
  8. at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:154)
  9. at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:52)
  10. at feign.Feign$Builder.target(Feign.java:251)
  11. at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:55)
  12. at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:238)
  13. at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:267)
  14. at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:247)
  15. at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171)
  16. ... 43 common frames omitted

2、错误原因

报错信息翻译过来就是:方法有太多的Body参数。根据错误提示信息,可以看到具体的是哪个方法,然后找到对应的方法,查看参数是否有误,当通过Feign客户端调用其他服务的接口时,请求参数可以有多个,用spring自带的注解用的方式不对,所以才导致报错。在同一个方法参数位置,@RequestParam注解可以用多个,但是@RequestBody注解只能用一个。

以下是我项目里用的方式,String devModel, String eventId这两个参数没有用@RequestParam注解说明,导致上述报错。

  1. @ApiOperation("根据模型事件查询消息规则")
  2. @RequestMapping(value = "/pushRule/getByEventId",method = RequestMethod.GET)
  3. public CommResponse<List<PushRuleResponse>> getByEventId(String devModel, String eventId);

3、解决方案

解决方法其实很简单,在方法的参数上加上@RequestParam注解(同时可以用多个)即可。这里有两个参数,加上两个@RequestParam注解。如果有一个对象参数是要用@RequestBody注解的(这个注解同时只能用一个)。

  1. @ApiOperation("根据模型事件查询推送规则")
  2. @RequestMapping(value = "/pushRule/getByEventId",method = RequestMethod.GET)
  3. public CommResponse<List<PushRuleResponse>> getByEventId(@RequestParam String devModel, @RequestParam String eventId);

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/296580?site
推荐阅读
相关标签
  

闽ICP备14008679号