java - 使用 Spring Boot 2 WebClient 从响应中获取标头
问题描述
我想从 webclient 响应中接收标头(尤其是内容类型)。我用 flatmap-mono-getHeaders 找到了这段代码,但它不起作用。
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'image/tiff' not supported for bodyType=org.company.MyResponse
我该如何解决?或者也许有人可以推荐一个更简单的解决方案。
Mono<Object> mono = webClient.get()
.uri(path)
.acceptCharset(StandardCharsets.UTF_8)
.retrieve()
.toEntity(MyResponse.class)
.flatMap(entity -> Mono.justOrEmpty(entity.getHeaders().getFirst("content-type")));
Object rs = mono.block();
public class MyResponse {
Object body;
Integer status;
String contentType;
}
解决方案
我想从 webclient 响应中接收标头(尤其是内容类型)
一般来说,您可以像这样访问响应标头:
ResponseEntity<MyResponse> response = webClient.get()
.uri(path)
.acceptCharset(StandardCharsets.UTF_8)
.retrieve()
.toEntity(MyResponse.class)
.block();
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
但是从您粘贴的错误看来,您正在访问的图像 ( image/tiff
) 显然无法转换为您的MyResponse
类。
推荐阅读
- node.js - 如何在同一查询中从当前集合中获取对象数组和来自另一个集合的数据
- python - 从 torchvision 预训练模型中获取模型类标签
- angular - const {something} = Plugins 是什么意思?
- php - Laravel Query Builder Join vs With Function,哪个更好?
- python - 在不使用 insert() 的情况下将项目插入 Python 列表
- javascript - 遍历订单记录并将重复值连接成字符串
- javascript - 从回调中做出承诺
- javascript - 如何将 Web MIDI API 与 React 前端和 Django 后端集成
- kubernetes - 创建 Kubernetes 部署时出错
- swift - Google Classroom API 与 swift 的集成