zoukankan      html  css  js  c++  java
  • SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters

    目录

    1、报错信息

    2、feign多参数问题

    1.1 POST方式

    1.2 GET方式


    1、报错信息

    错误日志内容为:

    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

    2、feign多参数问题

    1.1 POST方式

    错误写法

    public int save(@RequestBody Person p, @RequestBody UserModel user);
    @ApiOperation("保存策略型号关系")
    @PostMapping("/openapi/tactics/insertTacticsModel")
    public CommResponse<?> insertTacticsModel(@RequestBody TacticsModel tm, @RequestBody("tacticsId") Integer tacticsId){}

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

    正确写法

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


    1.2 GET方式

    错误写法

    @RequestMapping(value="/test", method=RequestMethod.GET)  
    public Model test(String name, int age){}
     
    启动服务的时候,会报如下异常:

    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){}
     

  • 相关阅读:
    《将博客搬至CSDN》
    选课系统
    ATM_购物车
    python基础 面向对象编程
    python 基础 模块
    python基础 函数基础 模块:总复习
    第三篇:操作系统基础
    浅谈红黑树
    浅谈B和B+树
    第二篇:网络基础
  • 原文地址:https://www.cnblogs.com/no8g/p/13415539.html
Copyright © 2011-2022 走看看