Java 为什么RestTemplate GET response是JSON格式,而应该是XML格式?
我使用RestTemplate(org.springframework.web.client.RestTemplate)处理Extange spring行为,但没有成功 我在我的hole应用程序中使用下面的代码,并总是收到一个XML响应,我解析并评估它的结果Java 为什么RestTemplate GET response是JSON格式,而应该是XML格式?,java,spring,rest,resttemplate,Java,Spring,Rest,Resttemplate,我使用RestTemplate(org.springframework.web.client.RestTemplate)处理Extange spring行为,但没有成功 我在我的hole应用程序中使用下面的代码,并总是收到一个XML响应,我解析并评估它的结果 String apiResponse = getRestTemplate().postForObject(url, body, String.class); 但无法理解执行以下操作后服务器响应为何为JSON格式: String apiRe
String apiResponse = getRestTemplate().postForObject(url, body, String.class);
但无法理解执行以下操作后服务器响应为何为JSON格式:
String apiResponse = getRestTemplate().getForObject(url, String.class);
我已经在底层RestTemplate上调试过,内容类型是XML,但不知道为什么结果是JSON
当我从浏览器访问时,响应也是XML格式的,但在apiResponse中我得到了JSON
在阅读了Spring文档之后,我尝试了很多选择
还试图显式修改标题,但仍然无法理解
我调试了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);
}
}
}
public void doWithRequest(ClientHttpRequest请求)引发IOException{
if(responseType!=null){
List allSupportedMediaTypes=new ArrayList();
对于(HttpMessageConverter messageConverter:getMessageConverters()){
if(messageConverter.canRead(responseType,null)){
List supportedMediaTypes=messageConverter.getSupportedMediaTypes();
对于(MediaType支持的MediaType:supportedMediaTypes){
如果(supportedMediaType.getCharSet()!=null){
支持的媒体类型=
新的MediaType(supportedMediaType.getType(),supportedMediaType.getSubtype());
}
添加(supportedMediaType);
}
}
}
如果(!allSupportedMediaTypes.isEmpty()){
MediaType.sortBySpecificity(所有支持的媒体类型);
if(logger.isDebugEnabled()){
debug(“将请求接受头设置为”+allSupportedMediaTypes);
}
request.getHeaders().setAccept(所有支持的媒体类型);
}
}
}
你能给我一个主意吗?我可以在RC的帮助下解决我的问题。我会把答案贴出来帮助别人 问题是Accept header自动设置为APPLICATION/JSON,因此我必须更改调用服务的方式,以便提供我想要的Accept header 我改变了这一点:
String response = getRestTemplate().getForObject(url, String.class);
要使应用程序正常工作,请执行以下操作:
// 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();
//显式设置XML内容类型以强制XML响应(如果不是,spring将获得JSON响应)
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_XML));
HttpEntity=新的HttpEntity(“参数”,标题);
ResponseEntity response=getRestTemplate().exchange(url,HttpMethod.GET,entity,String.class);
字符串responseBody=response.getBody();
如果您使用的浏览器是Firefox,问题是accept标头,RestTemplate
级别较低,只需执行一个基本的GETRC即可,需要查看请求标头和内容响应类型来解决这个问题。请为您的问题提供更多的上下文。@RC,我刚刚用新信息更新了这篇文章。allSupportedMediaTypes似乎总是与application/json一起使用。如果通过调试器将其修改为application/xml,则可以工作。为什么会这样?顺便问一下,默认情况下如何使用application/xml?@Pedantic,之所以出现isse,是因为“allSupportedTypes”包含application/json,并将其添加到setAccept。我需要弄清楚设置json的原因以及如何更改它。谢谢,我也可以用上面提到的代码解决我的问题。谢谢联邦快递!对我来说,这不起作用,因为我的内容类型是application/json;UTF-8。无法提取响应:找不到响应类型的合适HttpMessageConverter…classname。。。内容类型[application/json;charset=UTF-8]第一次认为是转换器或“泛型”问题,但事实并非如此。你的解决方案很有魅力!至少对我来说,谢谢!设置期望值解决了我的问题:headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));谢谢你,伙计:)