I am using spring android rest template
and trying to convert a JSON response into a java class as shown here.
Here is my snippet trying to do the work:
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);
I think I have done everything required as it is told in the document:
url
really returns a JSON object.Result
class is a POJO with all the required fieldsBasically having the Jackson dependencies
in my classpath must be enough to get it work as it says:
The MappingJacksonHttpMessageConverter is conditionally loaded when you create a new RestTemplate instance. If the Jackson dependencies are found in your classpath, the message converter will be automatically added and available for use in REST operations.
So what I am missing here, what I am doing wrong?
Thanks
Full exception can be found here:
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
I've never done anything on Android but I know converts are picky with Content-Type. For Jackson to work the message body needs to have 'Content-Type: application/json'. Try to check all headers on the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With