问题描述
更新02/05/2018(大约4年后)...我再次对此进行了测试,因为人们一直在讨论我的问题/答案,Sotirios Delimanolis是正确的,我不应该写我的答案中的代码使这项工作。我使用了基本相同的RestTemplate / REST服务设置,如我的问题所示,REST服务具有已确认的响应内容类型application / json,而RestTemplate能够处理响应而没有问题进入Map。
Update 02/05/2018 (about 4 years later)...I tested this again as people have been upvoting my question/answer and Sotirios Delimanolis is correct that I should not have to write the code in my answer to make this work. I used basically the same RestTemplate/REST service setup as shown in my question with the REST service having a confirmed response content type of application/json and RestTemplate was able to process the response with no issues into a Map.
我正在调用一个返回 JSON 像这样:
I'm invoking a rest service that returns
JSON
like this:
{
"some.key" : "some value",
"another.key" : "another value"
}
我想我可以使用
java.util.Map
作为响应类型调用此服务,但这对我不起作用。我得到了这个例外:
I would like to think that I can invoke this service with a
java.util.Map
as the response type but that's not working for me. I get this exception:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map]
我应该只指定
String
作为响应类型并将 JSON
转换为 Map
?
Should I just specify
String
as the response type and convert the JSON
to a Map
?
编辑我
这是我的restTemplate电话:
Here's my restTemplate call:
private Map<String, String> getBuildInfo(String buildUrl) {
return restTemplate.getForObject(buildUrl, Map.class);
}
以下是我设置restTemplate的方法:
Here's how I'm setting up the restTemplate:
@PostConstruct
public void initialize() {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return execution.execute(requestWrapper, body);
}
});
restTemplate.setInterceptors(interceptors);
}
编辑II
完整错误消息:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:239) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at idexx.ordering.services.AwsServersServiceImpl.getBuildInfo(AwsServersServiceImpl.java:96) ~[classes/:na]
推荐答案
正如我之前所说,您的错误消息显示您正在接收
application / octet-stream
作为 Content-Type
。
As I had previously noted, your error message is showing us that you are receiving
application/octet-stream
as a Content-Type
.
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [application/octet-stream]
因此,杰克逊的
MappingJackson2HttpMessageConverter
无法解析内容(它预期 application / json
)。
As such, Jackson's
MappingJackson2HttpMessageConverter
cannot parse the content (it's expecting application/json
).
原始答案:
假设您的HTTP响应
Content-Type
是 application / json
你在类路径上有Jackson 1或者2, RestTemplate
可以将JSON反序列化为 java.util.Map
就好了。
Assuming your HTTP response's
Content-Type
is application/json
and you have have Jackson 1 or 2 on the classpath, a RestTemplate
can deserialize JSON like you have into a java.util.Map
just fine.
如果你没有完整地显示错误,你要么注册了自定义
HttpMessageConverter
对象,这些对象会覆盖默认值,或者你没有没有杰克逊在你的类路径和 MappingJackson2HttpMessageConverter
没有注册(这将进行反序列化)或你没有收到 application / json
。
With the error you are getting, which you haven't shown in full, either you've registered custom
HttpMessageConverter
objects which overwrite the defaults ones, or you don't have Jackson on your classpath and the MappingJackson2HttpMessageConverter
isn't registered (which would do the deserialization) or you aren't receiving application/json
.
这篇关于resttemplate getForObject map responsetype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!