openFeign接口方法传递多个参数时错误:nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract xxx
这是因为feign客户端的方法参数没有用相关的注解
@RequestParam
- 客户端@RequestParam注解的value属性必须指定值,要和服务端接口参数名保持一致
- 如果需要传递多个字符串参数,要使用多个@RequestParam注解与服务端接口参数保持对应
@RequestBody
- @RequestBody 注解在服务端和客户端都需要使用
- 参数名和参数类型在服务端和客户端需要保持一致
- 一般用于传递对象
例:
远程Controller
@RestController
@RequestMapping("/storage")
public class StorageController {
@Autowired
private StorageService storageService;
@RequestMapping("/decrease")
public CommonResult decrease(Long productId,Integer count){
storageService.decrease(productId,count);
return new CommonResult(200,"扣减库存成功");
}
}
FeignClient
@FeignClient("seata-storage")
@Component
public interface StorageService {
@PostMapping("/storage/decrease")
CommonResult decrease(@RequestParam(value = "productId") Long productId,
@RequestParam(value = "count") Integer count);
}
注:
openFeign默认使用@RequestBody,但只允许一个@RequestBody,所以:
- 只传递一个参数无需注解
- 传递多个参数时要用@RequestParam
若远程Controlle方法参数有@PathVariable注解,则FeignClient传递参数也必须用@PathVariable注解,且指定@PathVariable的value对应Controller。