【问题标题】:How to convert webclient response to ResponseEntity?如何将 webclient 响应转换为 ResponseEntity?
【发布时间】:2021-08-30 16:10:35
【问题描述】:

我可以使用现在已弃用的 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();
}

【问题讨论】:

标签: spring spring-boot reactive-programming spring-webflux


【解决方案1】:

我是通过以下方式做到的:

   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();
    }

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多