OpenFeign: Method has too many Body parameters

242 阅读1分钟

使用openFeign 进行优雅的RPC调用时,非常有可能遇到这个问题:

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 

这种报错,一般都是因为编写的注入服务接口未注解参数

/order/src/main/java/org/example/service/ProductService.java

特别关注 17行 : void reduceStock(Integer pid, Integer number);

错误示例:

package org.example.service;

import org.example.entities.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@FeignClient(value="service-product")
public interface ProductService {

    @RequestMapping("/product/{pid}")
    Product findByPid(@PathVariable Integer pid);

    @RequestMapping("/product/reduceStock")
    void reduceStock(Integer pid, Integer number);
}

正确示例:

package org.example.service;

import org.example.entities.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@FeignClient(value="service-product")
public interface ProductService {

    @RequestMapping("/product/{pid}")
    Product findByPid(@PathVariable Integer pid);

    @RequestMapping("/product/reduceStock")
    void reduceStock(@RequestParam("pid") Integer pid, @RequestParam("number") Integer number);

}