美文网首页
Spring Cloud微服务使用Feign进行服务间调用报错M

Spring Cloud微服务使用Feign进行服务间调用报错M

作者: 南湘嘉荣 | 来源:发表于2021-05-27 10:13 被阅读0次

    基于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)
    

    相关文章

      网友评论

          本文标题:Spring Cloud微服务使用Feign进行服务间调用报错M

          本文链接:https://www.haomeiwen.com/subject/xqztsltx.html