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.

Saturday, May 12, 2012

FUN things to DO - FunDo

So I am back after a long hiatus - time when I was so busy and stressed at work that I did not have the energy and inclination to write about programming when not working. But, finally, I am about to move out of this project and looking forward to a fresh beginning at a new place.

With work being less hectic in the past month and an interesting internal contest being launched my company, I had the opportunity to put on my thinking cap and go back to a framework I fell in love right at the beginning of my IT career - GWT. The result is an interesting little web application I have called FunDo.

As part of the contest, I was supposed to use OpenShift, Red Hat's PaaS service on the cloud which is where the application is currently hosted. The app is basically a mashup using the Google Places, Autocomplete and Geocoding APIs along with Google Maps v3.

The first version of the application code in the form of an Eclipse Helios project can be downloaded from here. Please note that this version of the project was called 'vacspothunter'. GWT v2.4 would be needed along with the GWT Plugin for Eclipse for compiling the project.

Keep watching this space for more posts detailing the main aspects of the design.