如何将 Web 客户端响应转换为 ResponseEntity?

Ash*_*Ash 0 spring reactive-programming spring-boot spring-webflux

我可以使用exchange()现已弃用的方法将 WebClient 响应转换为响应实体。

请建议达到相同结果的其他方法。下面是我的代码。

public ResponseEntity<TestClass> getTestDetails() {
            ClientResponse clientResponse = webClientBuilder.build()
                    .get()
                    .uri("http://localhost:9090/test")
                    .headers(httpHeaders -> {
                        httpHeaders.add(Constants.ACCEPT, Constants.APPLICATION_JSON);
                        })
                    .exchange()
                    .block();
            
            return clientResponse.toEntity(TestClass.class).block();
}
Run Code Online (Sandbox Code Playgroud)

Ash*_*Ash 5

我通过以下方式做到了:

   public ResponseEntity<TestClass> getTestDetails() {
        return webClientBuilder.build()
                .get()
                .uri("http://localhost:9090/test")
                .headers(httpHeaders -> {
                    httpHeaders.add(Constants.ACCEPT, Constants.APPLICATION_JSON);
                    })
                .retrieve()
                .toEntity(TestClass.class)
                .block();
    }
Run Code Online (Sandbox Code Playgroud)