spring - 如何使用 Spring 5 WebClient 进行头部调用
问题描述
我无法使用 WebClient 对休息服务进行 HEAD 调用。我只想读取计数标头,因为需要在进一步的其余调用中使用相同的标头,如何实现相同的实现如何将 Mono> 转换为整数计数值。GET 调用成功但是 HEAD 调用总是失败,我使用以下代码:
private void getResultCount(WebClient webClient)
{
Mono<ClientResponse> response = webClient.head()
.uri("/rest/v2/electronics/products/search/?query=::category:575:")
.exchange()
.doOnSuccess(clientResponse -> System.out.println("clientResponse.headers() = " + clientResponse.headers()));
System.out.println("response "+response);
}
解决方案
我认为您要问的是如何从 Head 调用中提取信息,当它的响应在 HEADERS 中而不是在正文中时,我需要类似的东西,过了一会儿我发现了这篇baeldung 文章
我的解决方案是:
var aHeader = aClient.head()
.retrieve()
.toEntity(String.class)
.map(aResponse -> aResponse.getHeaders())
.block();
System.out.println(aHeader);
推荐阅读
- javascript - 如何将我的菜单更改为切换菜单?
- amazon-web-services - AWS Athena 中整数数组的总和
- r - (RStudio) 带有 ggplot 的闪亮 plotOutput 不会产生任何结果
- java - 在计算大小为 K 的非连续子数组的总和时查找数组值
- c - 在 C 中使用链表时的指针问题
- elasticsearch - Kibana 在更新角色时收到此错误“不允许应用程序”?
- node.js - 在 node.js 中正确地流式处理生成进程的数据块
- javascript - 如何制作两个像切换按钮一样的按钮
- cocoa - 在 Swift 4.2 中 NSCollectionView 的 SupplementaryElementKind 字符串值中使用什么
- c# - 如何计算故事板动画循环的次数?