Spring WebClient正在检索错误的Content-Type

时间:2020-11-08 01:31:39

标签: spring webclient spring-webflux spring-webclient

当我按如下方式运行WebClient时:

WebClient.create(apiUrl)
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/some/{productId}/")
                        .queryParam("ws_key", apiKey)
                        .build(productId)
                )
                .accept(MediaType.APPLICATION_XML)
                .retrieve()
                .bodyToMono(MyClass.class)
                .block();

服务器返回标题Content-Type,其值为“ text / html”。

当我使用Chrome发出请求时,它会正确返回“ text / xml”(这是一个XML页面)。

我尝试了几个“ accept”参数,但我总是得到“ text / html”。

对这个问题有任何想法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用curl进行检查,并添加-H来设置标题。

curl http://xxx/some/product -H "Accept:text/xml"

如果得到相同的结果(html)。我认为服务器端应该有问题。

使用浏览器访问API时,我认为其accept标头默认设置为“ * / *,text / html”序列或类似设置。

如果服务器端由您自己控制,则显式设置响应主体的内容类型。

相关问题