如何在Spring webflux中获得原始响应作为字符串?


return webClient//
.post()//
.uri(whatever.com)
.header("Authorization", "Bearer " + authToken)//
.header("userId", CLIENT_ID)//
.header("clientRequestId", requestId)//
.bodyValue(bodyValue())//
.retrieve()//
.bodyToMono(Responseclass.class)//
.block();

以上是工作。假设我在调试,我只想把原始响应json转储到字符串中。我该怎么做呢?检索后的toString()不起作用,bodyToMono(String.class)似乎也不起作用。无论哪种方法,它都只是输出指针地址的默认toString值。

使用Spring Webflux WebClient,您可以获得像这样的字符串响应:

WebClient client = WebClient.create("http://someurl.de/something");
String responseBody = client.get().retrieve().toEntity(String.class)
.block().getBody();

在我的德语博客中我写了一篇关于使用WebClient的文章,在那里你可以找到更多关于上面代码片段的细节:
https://agile-coding.blogspot.com/2021/01/reactive-webclient.html

最新更新