我正在使用spring android rest template
并尝试将JSON响应转换为java类,如图here所示。
以下是我尝试完成工作的代码段:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
//I have already tried to manually setting converters with no luck
//List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
//messageConverters.add(new MappingJacksonHttpMessageConverter());
//messageConverters.add(new FormHttpMessageConverter());
//messageConverters.add(new StringHttpMessageConverter());
//restTemplate.setMessageConverters(messageConverters);
Result result = restTemplate.getForObject(url, Result.class);
我认为我已经完成了所需的一切,因为它在文件中被告知:
url
确实返回了一个JSON对象。Result
课程是包含所有必填字段的POJO 基本上在我的类路径中使用Jackson dependencies
必须足以使其工作,因为它说:
在创建新的RestTemplate实例时,有条件地加载MappingJacksonHttpMessageConverter。如果在类路径中找到Jackson依赖项,则会自动添加消息转换器并可用于REST操作。
那么我在这里缺少什么,我做错了什么?
由于
完整的例外情况可以在这里找到:
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): FATAL EXCEPTION: main
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): java.lang.IllegalStateException: Could not execute method of the activity
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.view.View$1.onClick(View.java:2083)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.view.View.performClick(View.java:2421)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.view.View$PerformClick.run(View.java:8869)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.os.Handler.handleCallback(Handler.java:587)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.os.Looper.loop(Looper.java:143)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.app.ActivityThread.main(ActivityThread.java:5068)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at dalvik.system.NativeStart.main(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): Caused by: java.lang.reflect.InvocationTargetException
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at xxx.SamplePageActivity.doRest(SamplePageActivity.java:83)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at android.view.View$1.onClick(View.java:2078)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): ... 11 more
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [[Lxxx.SamplePageActivity$Result;] and content type [application/json;charset=UTF-8]
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:77)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:202)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): ... 15 more
答案 0 :(得分:2)
我从未在Android上做过任何事情,但我知道转换者对于Content-Type是挑剔的。要让Jackson工作,消息正文需要'Content-Type:application / json '。尝试检查响应中的所有标头。
答案 1 :(得分:1)
我遇到了同样的例外并立即修复了它。
根本原因
在服务器上,我设置了这样的响应:
response.setContentType("text/html;charset=utf-8");
<强>解决方案强>
我像这样更新了contentType:
response.setContentType("application/json;charset=utf-8");
答案 2 :(得分:1)
对我来说,这个错误是针对某个类使用枚举类型而不是String来抛出某个属性的。
另一个原因也可能是您正在使用Jackson 2.然后您应该更改为MappingJackson2HttpMessageConverter
(而不是MappingJacksonHttpMessageConverter
)。
答案 3 :(得分:0)
在类似的情况下,这对我有用:
HttpHeaders requestHeaders = new HttpHeaders(); //发送 多部分/格式数据 requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
//填充要序列化的MultiValueMap和标题 用于请求的HttpEntity对象 HttpEntity&GT; requestEntity = new HttpEntity&gt;(parts,requestHeaders);
RestTemplate restTemplate = getRestTemplate(); restTemplate.getMessageConverters()。添加(新 FormHttpMessageConverter()); restTemplate.getMessageConverters()。添加(新 MappingJackson2HttpMessageConverter());
返回restTemplate.postForObject(URI,requestEntity, APIResponseUpload.class);