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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28815182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:51:50  来源:igfitidea点击:

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

javajsonspringrestresttemplate

提问by Manisha

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

我正在创建一个宁静的 API,它将使用来自服务器的 json。但我遇到了以下异常:

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)

org.springframework.web.client.RestClientException:无法提取响应:在 org.springframework 找不到适合响应类型 [[Lexamples.dto.DummyDTO;] 和内容类型 [text/json;charset=utf-8] 的 HttpMessageConverter。 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) 在 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)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)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)

Code snippet :

代码片段:

List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
msgConverters.add(new MappingHymansonHttpMessageConverter());
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 Hymanson message converter
            List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
            msgConverters.add(new MappingHymansonHttpMessageConverter());
            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

请告诉我我哪里出错了

采纳答案by Sergey Yamshchikov

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.

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