org.springframework.web.client.RestClientException: Could not extract response:
Looks like you change content type for request, but "application/json" have to be in the response headers, and the fact that you still have the same exception tells that you have wrong media type "text/json" in the response, there are no such media type in HTTP. Just look at implementation of restTemplate.exchange("http://server.com",HttpMethod.GET, entity, UserDTO[].class);
there the problem should be.
Manisha
Updated on March 05, 2020Comments
-
Manisha about 4 years
I'm creating a restful API which will consume json from server. But i'm getting the foll exception :
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [[Lexamples.dto.DummyDTO;] and content type [text/json;charset=utf-8] at 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(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:189) at 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)
Code snippet :
List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters(); msgConverters.add(new MappingJacksonHttpMessageConverter()); restTemplate.setMessageConverters(msgConverters); DummyDTO[] dummy= restTemplate.getForObject(URI, DummyDTO[].class);
Controller method code :
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(); }
Please tell me where am i going wrong
-
Manisha about 9 yearsyes the problem was the response type. once i changed it to application/json, things worked! Thanks!