Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Examples to make RestTemplate work with JSON?

  1. #11
    Join Date
    Nov 2010
    Posts
    15

    Default

    I dont see you taking use of the Event and Location objects in the 2nd code frame.

    You just download the resource and parse them to String with the help of RestTemplate. Later on u parse the String "event" into a JSON object afterwards which apparently doesnt correspond to your Event and Location objects.

    You also dont need to
    Code:
    messageConverters.add(new MappingJacksonHttpMessageConverter());
    since only use the StringMessageConverter.

  2. #12

    Default Re: Examples to make RestTemplate work with JSON?

    I don't know why you feel the need to deserialize the object but here you go:

    Code:
    GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();
    
    RestTemplate restTemplate = new RestTemplate();
        	
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new FormHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    restTemplate.setMessageConverters(messageConverters);
    		
    restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory());
    String url = "http://10.1.1.1:9998/event/1";
    String eventAsString = restTemplate.getForObject(url, String.class);
    		
    Event event = gson.fromJson(eventAsString, Event.class);
    	
    Log.d(TAG, "Result: [" + event + "]");
    The org.json.* package does not have a serializer/deserializer so you have to go with something like GSON.

  3. #13
    Join Date
    Nov 2010
    Posts
    15

    Default

    We cant go with just a JSON Object or HashMap, because we want to give those objects some more logic in form of additional methods.

    Ok, it's good to know about GSON. But why are you not just using the method restTemplate.getForObject(url,Event.class) in combination with the JacksonJSONMessageConverter? RestTemplate provides all necessary utils.

  4. #14

    Default Re: Examples to make RestTemplate work with JSON?

    You can, or you can use one of six different scenarios to deserialize this response. It is all up to you want you want to use.

    Perry Hoekstra

Posting Permissions

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