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 更多代码
}

 

完结!

 


相关文章
|
10月前
|
Android开发
复杂项目即时通讯从android 5升级android x后遗症之解决报错#79 java.io.EOFException Unexpected end of ZLIB input stream-优雅草卓伊凡|bigniu
复杂项目即时通讯从android 5升级android x后遗症之解决报错#79 java.io.EOFException Unexpected end of ZLIB input stream-优雅草卓伊凡|bigniu
577 4
复杂项目即时通讯从android 5升级android x后遗症之解决报错#79 java.io.EOFException Unexpected end of ZLIB input stream-优雅草卓伊凡|bigniu
|
9月前
|
Java API 开发工具
【Azure Developer】Java代码实现获取Azure 资源的指标数据却报错 "invalid time interval input"
在使用 Java 调用虚拟机 API 获取指标数据时,因本地时区设置非 UTC,导致时间格式解析错误。解决方法是在代码中手动指定时区为 UTC,使用 `ZoneOffset.ofHours(0)` 并结合 `withOffsetSameInstant` 方法进行时区转换,从而避免因时区差异引发的时间格式问题。
405 4
|
8月前
|
JSON Java 数据格式
java调用服务报错400
java调用服务报错400
221 6
java调用服务报错400
|
8月前
|
JSON Java 数据格式
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
506 6
|
SpringCloudAlibaba API 开发者
新版-SpringCloud+SpringCloud Alibaba
新版-SpringCloud+SpringCloud Alibaba
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
597 1
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
Java Nacos Sentinel
Spring Cloud Alibaba:一站式微服务解决方案
Spring Cloud Alibaba(简称SCA) 是一个基于 Spring Cloud 构建的开源微服务框架,专为解决分布式系统中的服务治理、配置管理、服务发现、消息总线等问题而设计。
3237 13
Spring Cloud Alibaba:一站式微服务解决方案
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
5547 2

热门文章

最新文章