Wednesday, May 16, 2012

Places API and Jackson JSON

This is the first in the series of posts I plan to write on my application FunDo, which was introduced in my last post. The backbone of the application is the Google Places API. Though I have had extensive experience working with XML, the API recommended using JSON. I thus started a search for a Java library that would help me easily convert the API JSON response to Java objects. Jackson JSON was where my search ended.

So to convert the Places API JSON response to Java Beans aka Value Objects (VO), you just have to follow these simple steps -
  1. Create a VO corresponding to the top level element in the JSON response, wrapping the element's children. 
  2. Each child element should be represented by Java reference variable within the parent VO with a name matching the element name in JSON. Depending on the JSON being processed, the child elements could either be simple or user defined types.
    For our example, we would create a parent VO called PlaceServiceReponseVO. The VO class name can be arbitarily selected. Needless to say, this being a Java bean, should have the getters and setters for all the member variables to be mapped.
  3. Instantiate the ObjectMapper class from Jackson API.
  4. Invoke the mapper.readValue(connection.getInputStream(), PlaceServiceResponseVO.class) and assign the returned value to a PlaceServiceReponseVO reference. Here the 'mapper' is the instance from step 3 and 'connection' is a javax.net.ssl.HttpsURLConnection object pointing to the Places API request URL.
  5. Add @JsonIgnoreProperties(ignoreUnknown = true) Jackson annotation to VO classes where you do not want to have all the fields from JSON mapped.
The following sample class has all the code you would need to invoke the Places API and map the response to Java objects -

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import com.fasterxml.jackson.databind.ObjectMapper;


public class PlacesAPISampleInvoker {
 
 /**
  * Places API URL request. Please replace the API_ACCESS_KEY with the your specific key from the Google API Console.
  */
 private static final String PLACES_API_SEARCH_REQUEST_URL = "https://maps.googleapis.com/maps/api/place/search/json?" +
   "sensor=false&location=19.03304880,73.02966250&radius=10000&types=bar&key=API_ACCESS_KEY";
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  try {
   URL url = new URL(PLACES_API_SEARCH_REQUEST_URL);
   HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
   ObjectMapper mapper = new ObjectMapper();
   PlaceSearchResponseVO searchResponseVO = mapper.readValue(connection.getInputStream(), PlaceSearchResponseVO.class);
   System.out.println(searchResponseVO.getStatus());
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } 
 }
}

For more on the VO definitions and to run this class, download the full eclipse project Java project.

No comments:

Post a Comment