基于Spring Cloud的微服务,在使用Feign接口的时候,对于接口方法多参数的场景需要特殊处理。否则,可能就会报错:
Caused by: java.lang.IllegalStateException: Method has too many body parameters
1.Get请求的Feign接口方法多参数处理
错误的方式:
@GetMapping("/ingredient/findIngredientByNameAndType")
Ingredient getIngredientByNameAndType(String name, String type);
正确的方式:
@GetMapping("/ingredient/findIngredientByNameAndType")
Ingredient getIngredientByNameAndType(@RequestParam("name") String name, @RequestParam("type") String type);
对于Get请求方法,大于等于两个参数的方法需要使用 @RequestParam 注解修饰。
2.Post请求的Feign接口方法多参数处理
错误的方式:
@PostMapping
public Taco saveTaco(@RequestBody Taco taco, @RequestBody Ingredient ingredient)
Feign接口方法中可以有多个@RequestParam 注解,但是@RequestBody注解只能有一个。
正确的方式:
@PostMapping(consumes = "application/json")
public Taco saveTaco(@RequestBody Taco taco, Long ingredientId, String ingredientName)
网友评论