java - WebClient 的 bodyToMono 错误
问题描述
我正在使用 Spring 5 WebClient 进行外部 api 调用,并希望将响应映射到这样的对象:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Response {
private long length;
}
private Mono<Response> getResponse() {
return webClient.get()
.uri("someURI")
.accept(MediaType.APPLICATION_JSON_UTF8)
.retrieve()
.bodyToMono(Response.class);
}
但我收到一个错误:
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported
但是,如果我像这样将响应正文提取到 String :
private Mono<String> getResponse() {
return webClient.get()
.uri("someURI")
.accept(MediaType.APPLICATION_JSON_UTF8)
.retrieve()
.bodyToMono(String.class);
}
然后它可以正常工作。任何想法如何解决这个问题?
编辑:
回复正文:
{
"blocks": [
{
"height": 545551,
"size": 48289,
"virtualSize": 48289,
"hash": "000000000000000000541d265115ec188544420c4b0e5dff6f2171e17e4991c9",
"time": 1535551238,
"txlength": 80,
"poolInfo": {}
}
],
"length": 1,
"pagination": {
"next": "2018-08-30",
"prev": "2018-08-28",
"currentTs": 1535587199,
"current": "2018-08-29",
"isToday": true,
"more": true,
"moreTs": 1535587200
}
}
标题:
status: 200 OK
解决方案
如果 HTTP 响应没有Content-Type
标头,客户端将假定application/octet-stream
,这不是 Spring WebFlux 中的 Jackson 编解码器支持的 Content-Type。
因此,您看到的行为是预期的行为,应该修复 Web API 以添加响应标头。
推荐阅读
- c++ - 如何在 Windows API Mouse Hook 中使用 UIAutomation Element 了解元素信息
- javascript - 将 Gremlin-Javascript 输出转换为 JSON
- node.js - Puppeteer - 在 page.goBack() 之后 for 循环不会继续第二次迭代
- flutter - Flutter/Dart - 滑动动画的反向?
- html - 想要将从 post 请求收到的 XML 显示为 HTML
- jquery - RoR:link_to 块中的 link_to 标签?
- visual-studio - 如何使用 asp:MenuItem NavigateUrl 属性在同一解决方案中调用 BlazorServer 应用程序?
- javascript - Clean webpack 插件在保存 sass 文件时清除字体和图像并且它们不会被重建
- javascript - 覆盖平面列表列数的下拉列表
- python - 在熊猫数据框中将字符串拆分为 3(三元组)