【问题标题】:Feign Client + Eureka POST request bodyFeign Client + Eureka POST 请求体
【发布时间】:2016-05-17 19:39:30
【问题描述】:

我正在尝试使用 Feign 和 Eureka 将发布请求从服务器 A 转发到服务器 B。两个服务器都被 Eureka 成功发现。

这行得通:

@Feignclient
public interface MyFeignClient {
    @RequestMapping(value = "test", = RequestMethod.POST, consumes = "application/json")
    ResponseEntity<String> theActualMethod(
            HttpServletRequest request,
            @RequestHeader("firstHeader") String header1,
            @RequestHeader("secondHeader") byte[] header2);
}

但是,当我将第二个参数更改为 @RequestBody 以读取 POST 请求内容时,出现异常:

java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity MyFeignClient.theActualMethod(javax.servlet.http.HttpServletRequest,java.lang.String,byte[])

【问题讨论】:

    标签: spring rest microservices netflix-eureka netflix-feign


    【解决方案1】:

    对我来说,问题在于我使用了@Param(如feign.Param)而不是@RequestParam(如org.springframework.web.bind.annotation.RequestParam)。将所有 @Param 更改为 @RequestParam 为我解决了这个问题。

    我不知道为什么会这样,但 Feign 存储库上的相关 question 可能会解释一下。

    【讨论】:

      【解决方案2】:

      问题在于 Feign 接口中的方法不能有多个“通用”参数。您可以拥有任意数量的标头参数,但不能超过主体。由于@RequestBody 不做任何事情,因此它不被视为标头,而是作为 HttpServletRequest 请求变量之外的另一个变量。

      所以我不得不将我的业务逻辑更改为只有一个参数。

      【讨论】:

      • 我在这里找到了我的方式,因为我在我的接口方法中从@Param("foo") String foo 中删除了所有@Param("foo"),因为我错误地认为它们是多余的:/
      • 这似乎是他们应该支持的。我也有同样的问题。找不到其他解决方案。
      猜你喜欢
      • 2020-02-16
      • 2020-02-22
      • 2017-07-14
      • 1970-01-01
      • 2021-01-06
      • 2016-12-16
      • 2012-11-09
      • 2018-10-15
      • 2021-12-17
      相关资源
      最近更新 更多