Andrej Koelewijn

  • Home
  • About
  • Departments
    • cloud
    • java
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • Uncategorized
    • web
  • Subscribe via RSS

Howto: Grails, REST, Google App Engine and JQuery

June 26th, 2009  |  Published in java, soa, web  |  4 Comments

Here’s a quick example how you can build a RESTfull Grails application and deploy it to Google App Engine. For this example you need Grails 1.1.1 and GAE SDK for Java version 1.2.1.

I’m going to create a small service which will return a wind forecast. The forecast data is hardcoded as this is just some sample data.

First create the grails application, remove the hibernate plugin, and install the app-engine plugin:

grails create-app windapp
cd windapp
grails uninstall-plugin hibernate
grails install-plugin app-engine

Make sure you’ve got your APPENGINE_HOME environment variable configured correctly. It should be something like:

export APPENGINE_HOME=/home/akoelewijn/programs/appengine-java-sdk-1.2.1

Next, I’ll create a controller which will implement a function to return the windforecast in JSON format:

grails create-controller Forecast

Edit the controller, and implement the method. The method just puts some dummy data into an array, which is returned as a JSON datastructure:

import grails.converters.*
class ForecastController {
    def show = {
    	def forecast = []
    	forecast << [ windspeed: 5, winddirection: 200 ]
    	render forecast as JSON
    }
}

Next, we need to edit the url mappings, calling the path /forecast will execute the method implemented above:

class UrlMappings {
    static mappings = {
        "/forecast"(controller:"forecast"){
            action = [GET:"show"]
        }
      "/"(view:"/index")
	  "500"(view:'/error')
	}
}

We have now implemented the service, which you can test, for example with curl. Test the application locally as follows:

grails app-engine run
curl http://localhost:8080/forecast

To test it on the google app engine, we first need to create an application. Log into Google app engine and create an application here: http://appengine.google.com/. For convenience, i’ve given it the same name as my grails application: windapp.

Now i can deploy the application:

grails set-version 1
grails app-engine package
$APPENGINE_HOME/bin/appcfg.sh update ./target/war

You should now have a running service. Again, we use curl to test it:

curl http://windapp.appspot.com/forecast

Now, using JQuery, i’ll create a simple html page to test the service. Create the webpage: web-app/index.html. To keep it simple, i’m using a google hosted version of jQuery, and i’m including all the javascript code in the page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Forecast service test</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    	google.load("jquery", "1");
		google.setOnLoadCallback(function(){
			$("#getForecast").click(getForecast);
		});
		function getForecast(){
			$.ajax({
				type:"GET",
				url:"/forecast",
				success:function(json){
					$("#forecastResults").text("Wind speed: " + json[0]["windspeed"] +
                                        ", direction: " + json[0]["winddirection"]);
				},
			    dataType: "json",
			    contentType: "text/json"
			});
		}
    </script>
  </head>
  <body>
  	<h1>Forecast tester</h1>
  	<button id="getForecast">Get Forecast</button>
  	<div id="forecastResults"></div>
  </body>
</html>

Redeploy the application to app engine:

grails app-engine deploy

Here’s the resulting page: windapp. Just push the button to call the REST service.

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • SphereIt
  • StumbleUpon
  • Technorati
  • LinkedIn
  • TwitThis
  • HackerNews
  • PDF

7 Tweets

Responses

Feed Trackback Address
  1. snaglepus says:

    June 30th, 2009 at 12:57 am (#)

    Howto: Grails, REST, Google App Engine and JQuery :: Andrej Koelewijn http://ff.im/-4B3td

    This comment was originally posted on Twitter

  2. jquerystories says:

    June 30th, 2009 at 1:08 am (#)

    Howto: Grails, REST, Google App Engine and JQuery :: Andrej Koelewijn

    [ http://www.andrejkoelewijn.com ]

    [.. http://bit.ly/Wmwmn

    This comment was originally posted on Twitter

  3. linksgoogle says:

    June 30th, 2009 at 1:16 am (#)

    Howto: Grails, REST, Google App Engine and JQuery :: Andrej Koelewijn http://tinyurl.com/pw8b37

    This comment was originally posted on Twitter

  4. VincentBostoen says:

    June 30th, 2009 at 1:24 pm (#)

    Howto: Grails, REST, Google App Engine and JQuery :: Andrej Koelewijn (via feedly) http://ff.im/-4CjJU

    This comment was originally posted on Twitter

Leave a Response

Additional comments powered by BackType

Tags

bi bpel camel cep css dsl esb esper google governance grails groovy gtalk html5 innovation internet ivy jabber java javascript jaxrs jersey jigsaw jquery linkeddata linux middleware mule noiv openoffice openweb oracle osgi oss plsql rdbms rest services smack soa sql sun web 2.0 xmpp yql

Archives

  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • SOA Governance: implicit or explicit
  • Chrome OS – because you have more than one computer
  • Git presentation JFall 2009
  • Neither smartphone nor app-phone: net-phone
  • Wicket and Mule on OC4J

Categories

  • cloud
  • java
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • Uncategorized
  • web

Recent Comments

  • andrkoel on SOA Governance: implicit or explicit
  • Chromebook on Chrome OS – because you have more than one computer
  • komarios on Oracle buying Sun: what does it mean for Open Source?
  • andrkoel on Chrome OS – because you have more than one computer
  • KarlP on A simple file monitoring console with camel, cometd and jquery

RSS Friendfeed

  • got a q yesterday where operations fits into soa - amazon answer: http://bit.ly/5J2t54, part of the service team: you build it , you run it December 16, 2009
    andrej koelewijn got a q yesterday where operations fits into soa - amazon answer: http://queue.acm.org/detail..., part of the service team: you build it , you run it 10 hours ago from Twitter - Comment - Like […]
    FriendFeed
  • Jeff Sutherland @ Google, Dec 14 2009 December 16, 2009
    andrej koelewijn Jeff Sutherland @ Google, Dec 14 2009 - http://jeffsutherland.com/scrum... 10 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • Spring 3.0: Java 5 Required, Adds New Expression Language and REST Support December 16, 2009
    andrej koelewijn Spring 3.0: Java 5 Required, Adds New Expression Language and REST Support - http://www.infoq.com/news... 10 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • scrum + cmmi 5 to improve productivity - Jeff Sutherland @ Google, Dec 14 2009 http://bit.ly/8MTbuT December 16, 2009
    andrej koelewijn scrum + cmmi 5 to improve productivity - Jeff Sutherland @ Google, Dec 14 2009 http://jeffsutherland.com/scrum... 10 hours ago from Twitter - Comment - Like […]
    FriendFeed
  • @Connect2ITeye twitter security is leaky: de wereld kan jouw tweets niet lezen, maar wel onze antwoorden... December 16, 2009
    andrej koelewijn @Connect2ITeye twitter security is leaky: de wereld kan jouw tweets niet lezen, maar wel onze antwoorden... 11 hours ago from Twitter - Comment - Like […]
    FriendFeed
  • Stronger, Better, Faster Design with CSS3 December 16, 2009
    andrej koelewijn Stronger, Better, Faster Design with CSS3 - http://www.smashingmagazine.com/2009... 11 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • Google Releases Fusion Tables API for Visualizing and Sharing Data December 16, 2009
    andrej koelewijn Google Releases Fusion Tables API for Visualizing and Sharing Data - http://blog.programmableweb.com/2009... 18 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • SCA Spring in Weblogic 10.3.2 & Soa Suite 11g Part 2 December 16, 2009
    andrej koelewijn SCA Spring in Weblogic 10.3.2 & Soa Suite 11g Part 2 - http://biemond.blogspot.com/2009... 18 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • iPhone developers abandoning app model for HTML5? December 16, 2009
    andrej koelewijn iPhone developers abandoning app model for HTML5? - http://scobleizer.com/2009... 18 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • Morgan Stanley: Mobile Internet Market Will Be Twice The Size of Desktop Internet December 16, 2009
    andrej koelewijn Morgan Stanley: Mobile Internet Market Will Be Twice The Size of Desktop Internet - http://www.readwriteweb.com/archive... 18 hours ago from Google Reader - Comment - Like […]
    FriendFeed


©2009 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.