【问题标题】:Rest Service Post Calls Not working with Spring Webclient休息服务发布调用不适用于 Spring Webclient
【发布时间】:2020-03-31 09:38:38
【问题描述】:

我需要异步调用一个休息服务,我想到了使用spring响应式的webclient而不是AsyncRestTemplate。但是,下面的代码根本没有调用我的 url。

Mono<Test> asyncResponse = webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .header("h1", h1).header("h2", h2)
                .body(BodyInserters.fromObject(request))
                .retrieve().bodyToMono(Test.class);

但是,如果我同步执行相同操作,一切正常。

webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .header("h1", h1).header("h2", h2)
                .body(BodyInserters.fromObject(request))
                .exchange();`

我做错了什么?

【问题讨论】:

    标签: java spring-boot spring-webflux spring-rest spring-webclient


    【解决方案1】:

    exchange 并不意味着同步。它响应Mono。你需要在某处subscribe()block() 你的流。

    exchangeretrieve 的区别在于:它们的返回类型不同; exchange 方法提供了一个 ClientResponse 及其状态、标头,而retrieve 方法是直接获取正文的最短路径。可以参考this

    【讨论】:

      猜你喜欢
      • 2016-04-20
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多