Skip to content

June 30, 2010

2

Using JSON in your Google App Engine Java app

If you’re using Java in your google app engine web service, you can use google’s GSON library to send JSON responses to your client.  To do that, you must first create a google app engine project. Then, get the GSON library from here: http://code.google.com/p/google-gson/.

Find the jar file (e.g. gson-1.4.jar) and place it in the war/WEB-INF/lib folder of your google web project. Now add the file to your build path. If you’re using Eclipse, you would do this by right-clicking on the project, then Build Path->Configure Build Path(Libraries Tab)->Add External JARs. Find the jar file you just added. Press ok and take a look at your project explorer.  You should see your new jar in the list of referenced libraries.

Eclipse project explorer Now you’re ready to use GSON.  I use it inside the class definition of the object I want to convert to JSON. In this example, I am using a class called “Response”, which simply contains two member variables: a status code and a message. But first, at the top of the file, I import the Gson library.

import com.google.gson.Gson;

public enum Status {
	ERROR, OK, WARNING
}
private Status status;
private String message;

public Response(Status status, String message)
{
    this.status = status;
    this.message = message;
}

Then I add the following two methods:

public String toJson() {
    	Gson gson = new Gson();
    	String json = gson.toJson(this);
    	return json;
    }
    public static Response constructFromJson(String json)
    {
    	Gson gson = new Gson();
    	return gson.fromJson(json, Response.class);
    }

Now you can use this in your HttpServlet response. Go to your doGet or doPut function and after doing any necessary processing, you can create and send your response like this:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
           throws IOException
    {
    	resp.setContentType("text/plain");
        //do your work here
	...
        PrintWriter out = resp.getWriter();
        Response response = new Response(Response.Status.OK, "A success!");
        out.print(response.toJson());
    }

The response that your client will receive looks like this:  {“status”:”OK”,”message”:”A success”}

*Troubleshooting: Make sure you use print and not println! I accidentally had the println in there from and earlier example, and this added a ‘\n‘ to my JSON string, which confused the parser and created a bug that took forever to find!

Read more from Google App Engine
2 Comments Post a comment
  1. Ken
    Nov 18 2012

    Nice post. Got me up and going THANKS!.

    Perhaps Content-Type: application/json would be better?

  2. Dec 23 2012

    GSON is awesome! I’d been searching for a good JSON library for use in GAE, and I believe my search is over. One important step, which you do mention, but seems to be a common mistake people make when adding libraries like this to their GAE project, is to forget to not only import the external .jar to their project build path, but also to paste the same jars in the war/WEB-INF/lib directory. Without it, things just won’t work, although you’d still get the nice support from your IDE when referencing the library.

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments