为了账号安全,请及时绑定邮箱和手机立即绑定

WebClient 的 bodyToMono 错误

WebClient 的 bodyToMono 错误

守着星空守着你 2021-08-19 18:27:48
我正在使用 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但是,如果我像这样将响应正文提取到字符串中:private Mono<String> getResponse() {return webClient.get()        .uri("someURI")        .accept(MediaType.APPLICATION_JSON_UTF8)        .retrieve()        .bodyToMono(String.class); }然后它可以正常工作。任何想法如何解决这个问题?
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

如果 HTTP 响应没有Content-Type标头,则客户端将假定application/octet-stream,这不是 Spring WebFlux 中的 Jackson 编解码器支持的 Content-Type。

因此,您看到的行为是预期的行为,并且应该修复 Web API 以添加响应标头。


查看完整回答
反对 回复 2021-08-19
  • 1 回答
  • 0 关注
  • 938 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信