Jump to content

Deploy Java RESTful Application on the Google App Engine

0
  Adam Musial-Bright's Photo
Posted May 29 2011 12:18 PM

I am a big fan of the Google App Engine (GEA) - which for me is the best PAAS on the market. The native support of Java, Python and recently also Go makes it a popular choice. It is also possible to implement applications in languages like Scala, JRuby, Clojure, Groovie ... which all run on the Java Virtual Machine.

In this short article you will learn all the steps needed to deploy a RESTful application on GAE implemented in Java and Jeryes REST framework.

1. First of all you need a Google account which after registration automatically allows you to deploy 10 applications

2. Next step is to install Eclipse IDE, GAE Eclipse plugin, and the Jersey REST framework. In Eclipse you will develop your application which you can easily deploy via the GAE Eclipse plugin. The Jersey REST framework supports you in RESTful services development.

3. After you run your Eclipse IDE with the GAE Eclipse plugin create a new Web Application Project and import all Jersey jars from the Jersey lib forlder (asm.jar, jersey*.jar, jsr311-api-1.1.1-jar, asm-3.1.jar and jackson*.jar) into the /war/WEB-INF/lib folder.

4. Configure Jersey in /war/WEB-INF/web.xml :

 <web-app>

   <init-param>
      <!-- speed up initial Jersey loading by deactivating WADL -->
      <param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
      <param-value>true</param-value>
    </init-param>

    <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>
          <!-- Enter your Jersey resources to speed up initial Jersey loading -->
          <!-- You can separate the java packages using , -->
          <param-value>com.your.api</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>Jersey Web Application</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>

 </web-app>


5. Test it with this simple REST service. Save the file in the package com.your.api, so the com.sun.jersey.config.property.packages from the web.xml example works out of the box.

 package com.your.api;

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

 @Path("/api/info")
 public class InfoResource {

   @GET
   @Produces("text/plain")
   public String info() {
     return "Hello Jersey on Google App Engine";
   }

 }


6. Run your project in Eclipse by hitting the Run button. GAE Eclipse plugin comes with the Jetty application server - which is also used by the GAE. Type in http://localhost:8080/api/info into your browser. You should see a "Hello" message. Now you can implement your application business logic and try it out this way.

7. Deploy your app in the Google App Engine. First you have to go to the Appengine Console and create a new app. Choose wisely here - you will not be able to change the app name later - but probably when you are done with your application you will map a domain to your app anyway.
The application name you choose - let's say myapp - will be accessible on http://myapp.appspot.com and also the ssl version https://myapp.appspot.com
Now edit /war/WEB-INF/appengine-web.xml and enter the application name here

 
  <application>myapp</application>
  <version>initial</version>


Now hit the button Deploy App Engine Project and after a moment you app will be online. From now on - you will purely focus on development - no server/infrastructure configuration needed - and that ist the beauty of PAAS.

Tags:
0 Subscribe


0 Replies