PDA

View Full Version : Android RestTemplate: How to assign the JSON Message converter



OneWorld123
Dec 14th, 2010, 11:51 AM
Thats my approach so far:



//relevant imports:
import org.springframework.http.converter.HttpMessageConv erter;
import org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter; //shipped w/ RestTemplate


The part w/ the error:


List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
mc.add(new MappingJacksonHttpMessageConverter()); //throws VerifyError
restTemplate.setMessageConverters(mc);

Thats the Error I am getting:

java.lang.VerifyError: org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter

dutchman_mn
Dec 14th, 2010, 12:46 PM
The problem is that MappingJacksonHttpMessageConverter internally references org.codehaus.jackson.map.ObjectMapper in a private variable.



private org.codehaus.jackson.map.ObjectMapper objectMapper;


The codehaus jackson library is not deployed part of the Spring Android RestTemplate code. If you put the *.jar file in your assets library (I used jackson-all-1.6.3.jar), it should work.

Why, do you ask did you get a VerifyError versus a ClassDefNotFound exception which would have pointed it out. I think because it is a private variable fully scoped rather than some like: private ObjectMapper objectMapper; where in this case there would have been an import statement.

Perry Hoekstra

OneWorld123
Dec 15th, 2010, 03:32 AM
Thats the way I fixed it with the help of dutchman:

I downloaded http://jackson.codehaus.org/1.6.3/jackson-all-1.6.3.jar from http://wiki.fasterxml.com/JacksonDownload dragged the .jar file in my directory for libraries files ("lib" is a common name for it) Added this file to build path no error is thrown at this place anymore (this line isn't necessary at all now, after adding the jackson lib to build path)


You use the assets folder. Ok, that seem uncommon for me.

Maybe you have to rephrase this part. For now it sounds like it's not intended for me:


Why, do you ask did you get a VerifyError versus a ClassDefNotFound exception which would have pointed it out. I think because it is a private variable fully scoped rather than some like: private ObjectMapper objectMapper; where in this case there would have been an import statement.

Roy Clarkson
Dec 20th, 2010, 08:58 AM
You are correct that the Jackson library is not included in the RestTemplate jar. I put together a blog post and sample application for using Maven with Spring Android. it contains a simple pom.xml example for building an android app with Spring Android and Jackson dependencies. Hopefully that will help answer some questions with regard to the dependency management. I'll be incorporating some of this information into the documents for the next milestone release as well.

http://blog.springsource.com/2010/12/17/spring-android-and-maven-part-1/

barryku
Dec 21st, 2010, 07:40 PM
If you are trying the JSON example in the reference manual, here's what worked for me after a few hours of struggling. Hope this will save someone time.



RestTemplate rest = new RestTemplate();
rest.setRequestFactory(new CommonsClientHttpRequestFactory());
List<HttpMessageConverter<?>> mc = rest.getMessageConverters();
MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();

List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(new MediaType("text", "javascript"));
json.setSupportedMediaTypes(supportedMediaTypes);
mc.add(json);
rest.setMessageConverters(mc);

String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}";

Log.d("SpringAndroid", "Search Google");
JsonResult jsonResult = rest.getForObject(url, JsonResult.class,
"Spring Android");
StringBuilder result = new StringBuilder();
int i = 0;
for (ResultItem item : jsonResult.getResponseData().getResults()) {
result.append(++i).append('.').append(item.getTitl e()).append(":")
.append(item.getContent()).append("<br/><br/>");
}
Log.d("SprintDemo", result.toString());
TextView view = (TextView) findViewById(R.id.resultText);
view.setText(Html.fromHtml(result.toString()));


You will find more details at my blog, http://agilesc.barryku.com/?p=147 if the above is not clear.

makeurownrules
Jan 23rd, 2011, 03:14 AM
I hope by now you have resolved resolved your issue. I wrote a snippet which uses realtime restful service

<code>
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// The HttpComponentsClientHttpRequestFactory uses the
// org.apache.http package to make network requests
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

// The URL for making the GET request
final String url = "http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode={postalCode}&country={countryCode}&radius={radius}";

// Initiate the HTTP GET request, expecting an array of State
// objects in response
PostalCodes result = restTemplate.getForObject(url,
PostalCodes.class, this.postalCode, this.countryCode,
this.radius);
</code>

Still you have any issue then visit my blog at http://www.makeurownrules.com/rest-spring-maven-android

Cheers,
Kapil