为什么RestTemplate GET响应是在JSON中应该是XML?

Fed*_*zza 10 java rest spring resttemplate

我使用RestTemplate(org.springframework.web.client.RestTemplate)挣扎于一个额外的弹簧行为而没有成功.

我在代码下面的我的洞应用程序中使用并始终接收XML响应,我解析并评估其结果.

String apiResponse = getRestTemplate().postForObject(url, body, String.class);
Run Code Online (Sandbox Code Playgroud)

但无法弄清楚为什么服务器响应在执行后呈JSON格式:

String apiResponse = getRestTemplate().getForObject(url, String.class);
Run Code Online (Sandbox Code Playgroud)

我在低级RestTemplate调试,内容类型是XML,但不知道为什么结果是在JSON中.

当我从浏览器访问时,响应也是XML格式,但在apiResponse中我得到了JSON.

我试过很多选择阅读Spring文档后 http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html

还试图明确修改标题但仍然无法搞清楚.

我调试了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);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

你能说出一个主意吗?

Fed*_*zza 17

我可以通过RC解决我的问题.的帮助.我会发布答案来帮助其他人.

问题是Accept标头自动设置为APPLICATION/JSON所以我不得不改变调用服务的方式,以便提供我想要的Accept标头.

我改变了这个:

String response = getRestTemplate().getForObject(url, String.class);
Run Code Online (Sandbox Code Playgroud)

为此,以使应用程序工作:

// 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();
Run Code Online (Sandbox Code Playgroud)

  • 对我来说这不起作用,因为我的内容类型是application / json; UTF-8。无法提取响应:找不到适合于响应类型... classname ...和内容类型[application / json; charset = UTF-8]的HttpMessageConverter (2认同)