SpringBoot FeignClient方法有太多参数

时间:2017-04-25 07:51:53

标签: spring spring-cloud spring-cloud-feign

我使用spring-cloud的feignClient,当我的界面中有多个Beans参数时出现问题,错误显示如下:

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.wxl.pros.test.web.common.CommonResObject com.wxl.pros.test.web.feign.OrderFeignClient.createOrder(com.wxl.pros.test.services.form.order.CreateOrderForm,com.wxl.pros.test.services.vo.User)
    at feign.Util.checkState(Util.java:128)
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:114)
    at org.springframework.cloud.netflix.feign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133)
    at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64)
    at feign.hystrix.HystrixDelegatingContract.parseAndValidatateMetadata(HystrixDelegatingContract.java:34)
    at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146)
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53)
    at feign.Feign$Builder.target(Feign.java:209)
    at org.springframework.cloud.netflix.feign.HystrixTargeter.target(HystrixTargeter.java:48)
    at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:184)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
    ... 27 common frames omitted

这是我的一种界面方法:

@RequestMapping(value="/order/createOrder",method=RequestMethod.POST)
    public CommonResObject createOrder(@RequestBody OrderForm orderForm,@RequestBody User user);

3 个答案:

答案 0 :(得分:0)

如果您有一个请求正文和多个参数的多个参数。使用精确注释指定参数类型:

ResponseMessage<String> getCustomInformation((@RequestBody CustomRequest request, @RequestParam("language") String language,
            @RequestParam("channel") String channel, @RequestParam("requestId") String requestId

答案 1 :(得分:0)

您可以在第三个pojo中将它们放在一起:

public class Order {
     OrderForm orderForm;
     User user;
     public Order(@JsonProperty("orderForm") orderForm, @JsonProperty("user") User user) {
         this.orderForm = orderForm;
         this.user = user;
     }
}

然后您的API会像这样:

@RequestMapping(value="/order/createOrder",method=RequestMethod.POST)
public CommonResObject createOrder(@RequestBody Order order);

答案 2 :(得分:0)

@RequestBody只能存在一个。如果需要传输多个对象,则可以使用json合并多个对象。