gpt4 book ai didi

java - 使用 Spring Boot 2 WebClient 从响应中获取 header

转载 作者:行者123 更新时间:2023-12-02 18:32:11 29 4
gpt4 key购买 nike

我想从 webclient 响应中接收 header (尤其是内容类型)。我用 flatmap-mono-getHeaders 找到了这段代码,但它不起作用。

org.springframework.web.reactive.function.UnsupportedMediaTypeException:bodyType=org.company.MyResponse 不支持内容类型“image/tiff”

我该如何解决?或者有人可以推荐更简单的解决方案。

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

最佳答案

I would like to receive the headers (especially the content-type) from the webclient response

一般来说,您可以像这样访问响应 header :

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 类。

关于java - 使用 Spring Boot 2 WebClient 从响应中获取 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69283119/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com