org.springframework.web.client.RestClientException:无法提取响应:

时间:2015-03-02 16:51:09

标签: java json spring rest resttemplate

我正在创建一个从服务器上消耗json的restful API。但是我得到了foll例外:

  

org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型找到合适的HttpMessageConverter [[Lexamples.dto.DummyDTO;]和内容类型[text / json; charset = utf-8]           在org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)           在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454)           在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)           在org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:207)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)           在java.lang.reflect.Method.invoke(Method.java:597)           在org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite $ PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:189)           在org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)           在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)           在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)           在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)

代码段:

List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
msgConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(msgConverters);
DummyDTO[] dummy= restTemplate.getForObject(URI, DummyDTO[].class);

控制器方法代码:

public UserDTO[] getUserList(){
             List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
           acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

        // Set the Accept and Content type header
            HttpHeaders headers = new HttpHeaders();
           headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setAccept(acceptableMediaTypes);
            HttpEntity<?> entity = new HttpEntity<Object>(headers);

            // Add the Jackson message converter
            List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
            msgConverters.add(new MappingJacksonHttpMessageConverter());
            restTemplate.setMessageConverters(msgConverters);

            // Make the HTTP GET request, marshalling the response from JSON to an array of Users
            ResponseEntity<UserDTO[]> responseEntity  =   restTemplate.exchange("http://server.com",HttpMethod.GET, entity, UserDTO[].class);
            return responseEntity.getBody();
        }

请告诉我哪里出错了

1 个答案:

答案 0 :(得分:2)

看起来您更改了请求的内容类型,但是“application / json”必须位于响应标头中,并且您仍然具有相同异常的事实表明您在“text / json”中有错误的媒体类型响应,没有这样的media type in HTTP。只要看看问题应该在restTemplate.exchange("http://server.com",HttpMethod.GET, entity, UserDTO[].class);的实现。