【问题标题】:Error in REST call using WebClient in Spring Framework在 Spring Framework 中使用 WebClient 调用 REST 时出错
【发布时间】:2020-01-13 18:07:48
【问题描述】:

我正在尝试使用 WebClient 进行 REST 调用,但是我无法传递请求正文。它显示错误 - 类型捕获#1-of 的方法 syncBody(body) 未定义?

public static String getResult(Body body) {

    WebClient webClient = WebClient.builder()
            .defaultHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE)
            .build();

         String result= webClient.get()
                       .uri(URL)
                       .syncBody(body)
                       .retrieve()
                       .bodyToMono(String.class)
                       .block();


}

它显示错误 - 类型捕获 #1-of 的方法 syncBody(body) 未定义?

【问题讨论】:

  • get 请求中有正文吗?
  • 是的,有一个JSON作为payload,它是一个外部api
  • 我忘了谢谢你,我不得不将其更改为发布以使其正常工作。 @pedrohreis

标签: java rest spring-boot spring-webclient


【解决方案1】:

我可能遗漏了一些东西,但我认为您不能使用WebClient。实际上,在 Spring Boot 2.0.5 中,这甚至都没有编译。

发生的情况是,当您调用 .get() 时,您会得到一个不支持 bodysyncBody 方法的 RequestHeadersUriSpec 实例,而在调用 post(或 put 等)时你会得到一个RequestBodyUriSpec 的实例

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多