我創建了一個restful API,它將從服務器使用json。但是,我發現了FOLL異常:org.springframework.web.client.RestClientException:無法提取響應:
org.springframework.web.client.RestClientException:無法提取響應:沒有合適HttpMessageConverter找到的響應類型[[Lexamples.dto.DummyDTO;]和內容類型[ (org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:207) at sun.reflect.NativeMethodAccessorImpl.invoke0(納特IVE方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在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) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at 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();
}
請告訴我,我要去哪裏錯了
嘗試返回類型爲application/json而不是text/json,並提供控制器代碼。 – rishman 2015-03-02 18:03:08
@rishman要將內容類型更改爲application/json,我對現有方法進行了更改。仍然得到相同的錯誤。我編輯了我的帖子以包含控制器代碼 – Manisha 2015-03-03 06:19:32