Google App Engine + JAX-RS Jersey

This is a “how to” mini tutorial for running JAX-RS Jersey on Google App Engine Java environment.

Sandbox Environment:
Operating System:        Windows Vista
Integrated Development Environment: Eclipse Galileo

Setting up:

Start a Google App Engine project from Eclipse. Download Jersey 1.1.5-ea-SNAPSHOT from here . Explode the zip into a local folder. Grab the following jars and put them into GAEJ project’s /war/WEB-INF/lib folder.

asm-3.1.jar
jackson-core-asl-1.1.1.jar
jersey-client-1.1.5-ea-SNAPSHOT.jar
jersey-core-1.1.5-ea-SNAPSHOT.jar
jersey-json-1.1.5-ea-SNAPSHOT.jar
jersey-server-1.1.5-ea-SNAPSHOT.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar

HelloWorldResource.java

Following user-guide.pdf create a resource class as follow. You can download the Jersey user guide from here.

package com.dclonline.jerseytest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

//The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResources {

	// The Java method will process HTTP GET requests
	@GET
	// The Java method will produce content identified by the MIME Media
	// type "text/plain"
	@Produces("text/plain")
	public String getClichedMessage() {
		// Return some cliched textual content
		return "Hello World";
	}
}

web.xml

Edit web.xml as follow.

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
	<servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.dclonline.jerseytest</param-value>
        </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Test the service

After deployment to Google App Engine you can test the Hello World service using the following url.

http://<your google app engine app id>.appspot.com/resources/helloworld

A live link can be found here
http://meghbaksho22.appspot.com/resources/helloworld

Comments

  1. Razan Paul says:

    Nice………………..

  2. Ahmed says:

    Worked like a charm! Thanks Iqbal!

  3. Chris Pilsworth says:

    Thanks, this came in very helpful.

  4. Anish Sneh says:

    Great initiative for REST on GAE…
    Stay tuned… :)

  5. sairn says:

    Could you possibly provide a directory structure?

    While I’m certain that the example will work, I cannot determine where your sample code fits in to the standard eclipse “greeting” gwt code example.

    I am attempting to reconstitute your example using a gwt maven project with tomcat.

    Thanks
    s

  6. sairn says:

    Please ignore my earlier post.
    –I too quickly assumed/hoped — i.e., after seeing the GAE reference — that you were demontrating using GWT and Jersey together, as I’ve been researching how this is done. Thx, s

  7. E says:

    Hi,
    I am new in Java and GAE. Can you please upload your project-files somewhere? (github, fileshare, dropbox)

    I cant get this to work. Do I need a pom?
    Please expand the blog post with more info

  8. ibsta says:

    hey

    I understand that there were some problems running Jersey under Google’s App Engine. Specifically some problems with the JAX B package that Jersey uses.

    do you you know if this is still the case?

    I am trying to make a decision about which rest API to use at the moment

    cheers

    • iqbalyusuf says:

      I haven’t been playing with GAE and JAX-RS for a while. From reading GAEJ forum I do see people some time complaining about JAXB problem. However there are other XML serialization libraries (i.e. Simple XML) that seems to work just fine.

  9. Joseph says:

    Thanks for the post. It helped get me started!!!

  10. riaz says:

    Thank you very much!!!!!!
    I was really stuck with this for around 4 days.
    Thanks to you, you made my day.

  11. Very nice and simple. Easy to understand

  12. Michele says:

    Finally a working example =)
    Thanks a lot!

  13. Peter says:

    THAT WORKED!
    Lovely piece of tutorial. Simple & straight-forward.

  14. Sakey says:

    Haha, been searching sth like this for 2 week.
    It really help.
    Thanks,

Leave a Reply