Rest Service Post呼叫不适用于Spring Webclient

问题描述 投票:0回答:1

我需要异步调用rest服务,我想到了使用spring react的webclient而不是AsyncRestTemplate。但是,下面的代码根本不会调用我的网址。

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个回答
0
投票

exchange不表示同步。它响应Mono。我认为您需要subscribe()block()您的流。

exchangeretrieve的区别是:它们的返回类型不同;交换方法提供ClientResponse及其状态,标头,而检索方法是直接获取主体的最短路径。您可以参考this

© www.soinside.com 2019 - 2024. All rights reserved.