12

我使用 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);
4

3 回答 3

8

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

ResponseMessage<String> getCustomInformation((@RequestBody CustomRequest request, @RequestParam("language") String language,
            @RequestParam("channel") String channel, @RequestParam("requestId") String requestId
于 2018-05-30T11:41:14.030 回答
1

您可以将它们放在第三个 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);
于 2019-01-06T22:11:55.540 回答
0

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

于 2019-06-11T12:20:24.563 回答