android RestClientException:找不到合适的HttpMessageConverter
我正在使用spring android rest template
并试图将JSON响应转换为java类,如下所示。
这里是我的代码片段,试图完成这项工作:
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 在我的classpath中基本上具有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
我从来没有在Android上做过任何事情,但我知道转换对Content-Type很挑剔。 对于Jackson来说,消息主体需要具有“Content-Type:application / json ”。 尝试检查响应中的所有标题。
我遇到了同样的异常现在修复了它。
根本原因
在服务器上,我设置了这样的响应:
response.setContentType("text/html;charset=utf-8");
解
我更新了像这样的contentType:
response.setContentType("application/json;charset=utf-8");
对于我来说,这个错误是为一个类使用枚举类型而不是字符串为某个属性引发的。
另一个原因可能是您正在使用Jackson 2.您应该更改为MappingJackson2HttpMessageConverter
(而不是MappingJacksonHttpMessageConverter
)。
上一篇: android RestClientException: no suitable HttpMessageConverter found