Java 为什么 RestTemplate GET 响应在 JSON 中,而应该在 XML 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21613416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why RestTemplate GET response is in JSON when should be in XML?
提问by Federico Piazza
I struggled with an extrange spring behavior using RestTemplate (org.springframework.web.client.RestTemplate) without success.
我在使用 RestTemplate (org.springframework.web.client.RestTemplate) 时遇到了一个奇怪的弹簧行为,但没有成功。
I use in my hole application below code and always receive an XML response, which I parse and evaluate its result.
我在我的漏洞应用程序中使用下面的代码并且总是收到一个 XML 响应,我解析并评估它的结果。
String apiResponse = getRestTemplate().postForObject(url, body, String.class);
But can't figure out why a server response is in JSON format after executing:
但是无法弄清楚为什么执行后服务器响应是JSON格式:
String apiResponse = getRestTemplate().getForObject(url, String.class);
I've debugged at low level RestTemplate and the content type is XML, but have no idea why the result is in JSON.
我在低级别的 RestTemplate 上调试过,内容类型是 XML,但不知道为什么结果是 JSON。
When I access from a browser the response is also in XML, but in apiResponse I got JSON.
当我从浏览器访问时,响应也是 XML 格式,但在 apiResponse 中我得到了 JSON。
I tried many options after reading Spring documentation http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html
在阅读 Spring 文档http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html后,我尝试了很多选项
Also tried to modify explicitly the headers but still can't figure it out.
还尝试明确修改标题,但仍然无法弄清楚。
I debugged RestTemplate class and noticed that this method is always setting application/json:
我调试了 RestTemplate 类,注意到这个方法总是在设置 application/json:
public void doWithRequest(ClientHttpRequest request) throws IOException {
if (responseType != null) {
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
for (HttpMessageConverter<?> messageConverter : getMessageConverters()) {
if (messageConverter.canRead(responseType, null)) {
List<MediaType> supportedMediaTypes = messageConverter.getSupportedMediaTypes();
for (MediaType supportedMediaType : supportedMediaTypes) {
if (supportedMediaType.getCharSet() != null) {
supportedMediaType =
new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype());
}
allSupportedMediaTypes.add(supportedMediaType);
}
}
}
if (!allSupportedMediaTypes.isEmpty()) {
MediaType.sortBySpecificity(allSupportedMediaTypes);
if (logger.isDebugEnabled()) {
logger.debug("Setting request Accept header to " + allSupportedMediaTypes);
}
request.getHeaders().setAccept(allSupportedMediaTypes);
}
}
}
Could you give an idea?
你能给个主意吗?
采纳答案by Federico Piazza
I could solve my issue with RC.'s help. I'll post the answer to help other people.
我可以在 RC. 的帮助下解决我的问题。我会发布答案以帮助其他人。
The problem was that Accept header is automatically set to APPLICATION/JSON so I had to change the way to invoke the service in order to provide the Accept header I want.
问题是 Accept 标头会自动设置为 APPLICATION/JSON,因此我必须更改调用服务的方式才能提供我想要的 Accept 标头。
I changed this:
我改变了这个:
String response = getRestTemplate().getForObject(url, String.class);
To this in order to make the application work:
为此,为了使应用程序工作:
// Set XML content type explicitly to force response in XML (If not spring gets response in JSON)
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.GET, entity, String.class);
String responseBody = response.getBody();