SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters

简介: SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters

一、报错信息

错误日志内容为:

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.soft.framework.core.response.CommResponse com.soft.back.openplatform.api.AdvertApi.insertAdvertTactics(com.soft.back.openplatform.model.AdvertTactics,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.hystrix.HystrixFeign$Builder.target(HystrixFeign.java:70)
  at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallbackFactory(HystrixTargeter.java:64)
  at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:51)
  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

二、feign多参数问题

2.1 POST方式

错误写法

@ApiOperation("保存策略型号关系")
@PostMapping("/openapi/tactics/insertTacticsModel")
public CommResponse<?> insertTacticsModel(@RequestBody TacticsModel tm, @RequestBody("tacticsId") Integer tacticsId){
    // todo 更多代码
}

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

正确写法

@ApiOperation("保存策略型号关系")
@PostMapping("/openapi/tactics/insertTacticsModel")
public CommResponse<?> insertTacticsModel(@RequestBody TacticsModel tm, @RequestParam("tacticsId") Integer tacticsId, @RequestParam("modelIds") String modelIds){
    // todo 更多代码
}

2.2 GET方式

错误写法

@RequestMapping(value="/test", method=RequestMethod.GET)  
public Model test(String name, int age){
    // todo 更多代码
}

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

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.soft.framework.core.response.CommResponse com.soft.back.openplatform.api.AdvertApi.insertAdvertTactics(com.soft.back.openplatform.model.AdvertTactics,java.lang.String)

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

正确写法

@RequestMapping(value="/test", method=RequestMethod.GET)
public Model test(@RequestParam("name") String name, @RequestParam("age") int age){
    // todo 更多代码
}

 

完结!

 

相关文章
|
Web App开发 XML Java
java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters invo
Spring MVC 项目在使用 DeferredResult 实现异步接口时出现错误。 完整错误信息如下: 十一月 03, 2017 8:59:53 上午 org.apache.catalina.
2328 0
|
3月前
|
Java API Nacos
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forge
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forge
176 2
|
7月前
|
Java 应用服务中间件 Spring
SpringBoot出现 java.lang.IllegalArgumentException: Request header is too large 解决方法
SpringBoot出现 java.lang.IllegalArgumentException: Request header is too large 解决方法
242 0
|
Java Docker 微服务
【Java异常】Caused by: java.lang.IllegalArgumentException: method GET must not have a request body
【Java异常】Caused by: java.lang.IllegalArgumentException: method GET must not have a request body
203 1
SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters
SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters
235 0
|
7月前
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘quanZiController‘ method
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘quanZiController‘ method
52 0
|
7月前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
1741 0
|
Java Spring
【Java异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters
【Java异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters
166 0
|
Java
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
167 0
|
Java API
SpringBoot报错:java.lang.IllegalStateException: getReader() has already been called for this request
SpringBoot报错:java.lang.IllegalStateException: getReader() has already been called for this request
731 0