Results 1 to 6 of 6

Thread: Android RestTemplate: How to assign the JSON Message converter

  1. #1
    Join Date
    Nov 2010
    Posts
    15

    Default Android RestTemplate: How to assign the JSON Message converter

    Thats my approach so far:

    Code:
         
    //relevant imports:
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; //shipped w/ RestTemplate
    The part w/ the error:
    Code:
    List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
          mc.add(new MappingJacksonHttpMessageConverter()); //throws VerifyError
          restTemplate.setMessageConverters(mc);
    Thats the Error I am getting:
    Code:
    java.lang.VerifyError: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

  2. #2

    Default Re: Android RestTemplate: How to assign the JSON Message converter

    The problem is that MappingJacksonHttpMessageConverter internally references org.codehaus.jackson.map.ObjectMapper in a private variable.

    Code:
    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

  3. #3
    Join Date
    Nov 2010
    Posts
    15

    Default

    Thats the way I fixed it with the help of dutchman:

    1. I downloaded http://jackson.codehaus.org/1.6.3/jackson-all-1.6.3.jar from http://wiki.fasterxml.com/JacksonDownload
    2. dragged the .jar file in my directory for libraries files ("lib" is a common name for it)
    3. Added this file to build path
    4. 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:
    Quote Originally Posted by dutchman_mn View Post
    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.
    Last edited by OneWorld123; Dec 15th, 2010 at 08:13 AM.

  4. #4
    Join Date
    Nov 2010
    Posts
    174

    Default

    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...-maven-part-1/

  5. #5
    Join Date
    Aug 2004
    Posts
    1

    Default Working example

    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.

    Code:
    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.getTitle()).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.
    Last edited by barryku; Dec 22nd, 2010 at 12:33 AM.

  6. #6

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •