我只是想做一个简单的REST请求,如下所示
String url = "some url";
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
headers.add(HttpHeaders.AUTHORIZATION, "some authorization");
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Body body = new Body();
body.setRemarks("test");
org.springframework.http.HttpEntity request = new org.springframework.http.HttpEntity(body, headers);
try {
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
System.out.println("Status Response: "+ response.getStatusCode() +". Body: "+ response.getBody());
} catch (HttpClientErrorException | HttpServerErrorException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
问题是每次我在Java中这样做的时候,结果都是不可预测的,我通常都会得到这个错误。
org.springframework.http.InvalidMediaTypeException: Invalid mime type "text/plain, application/json, application/json, application/*+json, application/*+json, */*; charset=UTF-8": Invalid token character ',' in token "plain, application/json, application/json, application/*+json, application/*+json, */*"
at org.springframework.http.MediaType.parseMediaType(MediaType.java:452)
at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:745)
at org.springframework.web.client.DefaultResponseErrorHandler.getCharset(DefaultResponseErrorHandler.java:115)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
然而,当我通过邮递员尝试同样的请求时,它工作了。我遗漏了一些非常简单的东西,我不知道是什么。
我检查了邮递员和报头的响应,它们看起来像Content-Type →application/json; charset=UTF-8
一样有效,而且响应格式也很好。
我也不能确定请求或响应的哪一部分在哪个Rest模板上有问题。
发布于 2018-02-16 12:13:51
在我的例子中,简短的答案是在发出请求之前添加这些额外的标头。
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
headers.add(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.name());
很长的答案:由于Nikolay Shevchenko's建议使用调试器来挖掘服务器上的错误,RestTemplate
试图从响应中收集所有的头和正文来创建合理的异常消息。当尝试从响应创建该消息时,它获得了mime类型
text/plain, application/json, application/json, application/*+json, application/*+json, */*; charset=UTF-8
是的,这是整个文本长度,而不是Postman为同一请求显示的application/json; charset=UTF-8
。因此,在我使用较旧的spring rest模板版本的情况下,当它尝试parse over上面提供的mime类型时,它会失败,并产生以下消息。
org.springframework.http.InvalidMediaTypeException: Invalid mime type "text/plain, application/json, application/json, application/*+json, application/*+json, */*; charset=UTF-8": Invalid token character ',' in token "plain, application/json, application/json, application/*+json, application/*+json, */*"
因此,一个更细粒度的mime类型异常从更深的堆栈中冒出来,而不是获取服务器错误,这对您正在搜索的内容没有任何意义。
根据M. Deinum的this post,RestTemplate
是如何最终使用StringHttpMessageConverter
的,尽管我已经指定了如下所示的Jackson to Http消息转换器。
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
正如前面提到的,使用StringHttpMessageConverter
会导致writeAcceptCharset
为true
,这带来了几乎所有可能值的mime类型的长格式。在前面提到的帖子中,解决方案要么将writeAcceptCharset
设置为false
,要么不编写普通字符串,而是使用object,然后将其编组为字符串。
对于我的用例,我希望响应类型是带有UTF-8
字符集的application/json
,所以在配置中设置这些accept头就解决了我的问题。
https://stackoverflow.com/questions/48799409
复制相似问题