org.springframework.web.client.RestClientException:无法提取响应: [英] org.springframework.web.client.RestClientException: Could not extract response:

查看:2593
本文介绍了org.springframework.web.client.RestClientException:无法提取响应:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个从服务器上消耗json的restful API。但是,我发现了FOLL异常:

I'm creating a restful API which will consume json from server. But i'm getting the foll exception :

org.springframework.web.client.RestClientException:无法提取响应:没有合适HttpMessageConverter发现为响应类型[[Lexamples.dto.DummyDTO;]和内容类型[文本/ JSON;字符集= UTF-8]
。在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)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at jav a.lang.reflect.Method.invoke(Method.java:597)
在org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite $ PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:189)
。在组织。 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)
。在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)

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)

代码段:

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,在HTTP中没有这样的媒体类型。只看一下 restTemplate.exchange(http://server.com,HttpMethod.GET,entity,UserDTO [] .class)的实现; 那里的问题应该是。

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.

这篇关于org.springframework.web.client.RestClientException:无法提取响应:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆