spring - 通过 springs `WebClient` 进行api调用但忽略结果的正确方法是什么?

标签 spring spring-webflux

通过 springs 进行 api 调用的正确方法是什么 WebClient ,但忽略结果? ClientResponse对象特别指出我必须对结果做些什么......

Docs:

NOTE: When given access to a ClientResponse, through the WebClient exchange() method, you must always use one of the body or toEntity methods to ensure resources are released and avoid potential issues with HTTP connection pooling. You can use bodyToMono(Void.class) if no response content is expected. However keep in mind that if the response does have content, the connection will be closed and will not be placed back in the pool.



我可以进行 WebClient 调用并忽略结果吗?或者是否有一个通用的捕获所有“body 或 toEntity 方法”,我可以使用然后忽略?

最佳答案

在 Spring Framework 5.2 之前,使用 WebClient#exchange() 方法和直接处理Mono<ClientResponse>可能非常复杂或导致潜在的内存泄漏。
从 Spring Framework 5.2 开始,这对开发人员和 exchange() 来说变得更加容易了。方法已被弃用。

Mono<ResponseEntity<Void>> response = webClient.put()
     .uri("https://example.org/book/123")
     .retrieve()
     .toBodilessEntity();
Spring 将读取响应体(如果存在)并释放数据缓冲区,然后将连接返回到池中。它确保没有内存泄漏,包括使用额外的 Reactor 运算符时。

关于spring - 通过 springs `WebClient` 进行api调用但忽略结果的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312537/

相关文章:

Spring Webflux WebClient : Does it supports digest based authentication?

java - Spring Data JPA 查询不起作用,列不存在

java - 如何在 Spring Boot Webflux 中克隆 ServerResponse

java - 使用 Webflux 实现 POST

java - 从路由中删除方法只能通过 Postman 进行,不能通过浏览器进行

java - 如何自定义 SpringWebFlux WebClient JSON 反序列化?

java - 使用 Maven 的 Spring Boot 示例不会在代理后面编译

java - 来自 Angular 应用程序的请求被 Spring 应用程序中的 CORS 策略阻止

java - Camel : define jms queue name from properites file

spring - Docker:客户端和服务器之间的链接