Tuesday, June 15, 2010

RESTful on Google App Engine in 5

 RESTful on Google App Engine in 5

Quick prototyping and publishing is very useful in developing RESTful services.  It's essential that consumers can access and experiment with a service while it's being developed.  The Google App Engine (GAE) platform is free at the entry level, and provides sufficient capabilities to deploy RESTful applications with a public URI.

Prepare the Environment
  1. Obtain a free GAE account by visiting: appengine.google.com.
  2. From the "My Applications" view on the appengine page, create a new application ID.
  3. Install the Eclipse plugin .
Creating & Deploying a RESTful App on GAE
  1. In Eclipse, create a new web application (File / New / Web Application Project), notice the Google logo next to this option.
  2. Modify the appengine-web.xml file to reflect your new application ID:
    <application>your-app-id</application>



  3. Create a package for your REST classes


  4. Update web.xml with the following:


  5.     <servlet>
    <servlet-name>jersey</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>zpylx.restful</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>



  6. Download JAXB 2.1 ; note that 2.2 does not run on GAE at this time; unpack the file or, if your systems is unix-based, run:

    java -jar JAXB2_20081030.jar



  7. Download the JAX-RS reference implementation (jersey), and unpack.


  8. Place the JAXB and JAX-RS jars in the Eclipse project under /war/WEB-INF/lib.  Also, add the jars to the Java build path of the project.


  9. package zpylx.restful.action;

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


    @Path("/activitieslist")
    public class MapActivities {

    @GET
    @Produces("text/plain")
    public String getItems() {
    return "restful web service example";
    }



  10. Create RESTful classes, e.g.: Deploy to app engine from Eclipse, by clicking on the "Deploy App Engine Project" icon in the toolbar (installed by the GAE plugin).


  11. The application, based on the configuration above, would be accessible from this URI:  your-app-id.appspot.com/rest/activitieslist


  12. Manage application versions and view logs from the admin dashbord: appengine.google.com/dashboard?&app_id=your-app-id





example link: http://zhoupengylx.appspot.com/rest/activitieslist

1 comment:

  1. I was having some problems while I was trying to start with jersey. this really help me! greetings from Perú

    ReplyDelete