org.springframework.web.client.RestClientException: Could not extract response:

ぐ巨炮叔叔 提交于 2019-12-01 11:21:41

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/28815182/org-springframework-web-client-restclientexception-could-not-extract-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!