JAX-RS on Google App Engine

Last week I’ve been playing around with Google App Engine (GAE). Setting up the SDK with Eclipse and the Google Plugin for Eclipse was a breeze. Creating a simple webapp, testing it locally, and deploying it to appspot was a piece of cake. But then I figured I wanted to add a REST interface to my application…

I did not want to use GAE’s Google Cloud Endpoints – using the @Api annotation – because I didn’t want to tie myself to GAE. I wanted to use the JAX-RS standard so my application is portable across different Java application servers.

So the first option I looked at was Jersey, because I’ve use it before. But because of a incompatibility between asm versions (see stackoverflow for more details) I quickly decided to look for alternative JAX-RS implementations.

Next, I figured I’d give JBoss’ RESTEasy a try. But for some reason I just couldn’t get it to run on GAE. So after 10 minutes or so, I gave up.

So finally I decided to give Restlet a go, mainly because they offer a GAE edition. And within a couple of minutes I had a very basic REST service up and running. Here’s what I did:

  1. Fire up Eclipse and create a new Google Web Application project.
  2. Go to Restlet and download and unzip the GAE Edition of Restlet.
  3. Add the following jar files from the Restlet distribution to the war/WEB-INF/lib folder of the project and to its build path: javax.ws.rs.jar, org.restlet.jar, org.restlet.ext.servlet.jar, org.restlet.ext.jaxrs.jar, and org.json.jar.
  4. Add a very basic POJO:
  5. Add a simple REST service:
  6. Create the JAX-RS Application:
  7. Next, it is necessary to make the JAX-RS application known to Restlet by extending org.restlet.ext.jaxrs.JaxRsApplication
  8. Finally, configure your application to use Restlet in your web.xml deployment descriptor:

If you now start your application locally by selecting Run As > (Google) Web Application and point your web browser to http://localhost:8888/products you should see the following response:
{“name”:”Nexus 7″,”quantity”:999}

6 Replies to “JAX-RS on Google App Engine”

  1. Great article.

    I can’t get the POST to work though, I keep getting an error: “The given resource variant is not supported.”.
    I use restlet 2.1.4 and GAE 1.8.5. I’m using REST Console and Advanced REST client for Google Chrome as test client.

    Regards
    Brian

  2. Hi Brian,

    Did you specify the correct Content-Type in the Headers section of the Advanced Rest Client Application? There should be an HTTP header field named Content-Entry with the value application/json (Content-Type: application/json).

    Regards,
    Wim

  3. Hi Wim! Thanks for your article!
    I have the same issue of Brian, I don’t understand how to send a json post request.
    Could you give me the command of curl for testing the POST function?

    Regards,
    Federico

  4. Thank you Wim, I resolve adding these libraries in war/WEB-INF/lib directory:
    – org.codehaus.jackson.core.jar
    – org.codehaus.jackson.mapper.jar
    – org.restlet.ext.jackson.jar

    For testing it I use this curl command:
    curl -v -H “Accept: application/json” -H “Content-type: application/json” -X POST -d ‘{“name”:”xyz”, “quantity”:12}’ http://localhost:8888/products

    1. Thanks for the feedback. Guess I forgot to include the jar files you mention in the example >_o

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve + 7 =