为什么RestTemplate GET响应是在JSON中应该是XML? [英] Why RestTemplate GET response is in JSON when should be in XML?
问题描述
我在使用RestTemplate(org.springframework.web.client.RestTemplate)时遇到了一个额外的弹簧行为而没有成功。
I struggled with an extrange spring behavior using RestTemplate (org.springframework.web.client.RestTemplate) without success.
我在代码下方的洞应用程序中使用总是会收到一个XML响应,我会解析并评估其结果。
I use in my hole application below code and always receive an XML response, which I parse and evaluate its result.
String apiResponse = getRestTemplate().postForObject(url, body, String.class);
但无法弄清楚执行后服务器响应为何采用JSON格式:
But can't figure out why a server response is in JSON format after executing:
String apiResponse = getRestTemplate().getForObject(url, String.class);
我在低级RestTemplate调试,内容类型是XML,但不知道为什么结果是JSON。
I've debugged at low level RestTemplate and the content type is XML, but have no idea why the result is in JSON.
当我从浏览器访问时,响应也是XML格式,但在apiResponse中我得到了JSON。
When I access from a browser the response is also in XML, but in apiResponse I got JSON.
我在阅读Spring文档后尝试了很多选项
http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate。 html
I tried many options after reading Spring documentation 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.
我调试了RestTemplate类,并注意到此方法始终设置application / json:
I debugged RestTemplate class and noticed that this method is always setting 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?
推荐答案
我可以通过RC解决我的问题。的帮助。我将发布答案以帮助其他人。
I could solve my issue with RC.'s help. I'll post the answer to help other people.
问题是Accept标头自动设置为APPLICATION / JSON所以我不得不改变调用服务的方式为了提供我想要的Accept标头。
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.
我改变了这个:
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();
这篇关于为什么RestTemplate GET响应是在JSON中应该是XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!