Andrej Koelewijn

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

Howto: Grails, REST, Google App Engine and JQuery

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

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
  • HackerNews
  • PDF
  • Digg
  • Facebook
  • FriendFeed
  • Posterous
  • Tumblr
  • Twitter
  • RSS
  • VincentBostoen

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


    This comment was originally posted on Twitter

  • linksgoogle

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


    This comment was originally posted on Twitter

  • jquerystories

    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

  • snaglepus

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


    This comment was originally posted on Twitter

blog comments powered by Disqus

Tags

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

Archives

  • March 2010
  • February 2010
  • January 2010
  • 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

  • Getting started with Nexus maven repository manager
  • JEE CDI tip: Target Unreachable, identifier resolved to null
  • Absent Code attribute in method that is not native or abstract
  • Prezi presentation software needs to add visual
  • So you think it’s the iPad that’s missing features?

Categories

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

Recent Comments

  • andrej on So you think it’s the iPad that’s missing features?
  • Dave on So you think it’s the iPad that’s missing features?
  • andrej on So you think it’s the iPad that’s missing features?
  • Fernando on So you think it’s the iPad that’s missing features?
  • andrej on So you think it’s the iPad that’s missing features?

RSS Friendfeed

  • Amerikanen rangschikken Rotterdams MVRDV in top 50 March 9, 2010
    andrej koelewijn Amerikanen rangschikken Rotterdams MVRDV in top 50 - http://www.idealize.nl/2010... 15 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • Getting started with Nexus maven repository manager March 9, 2010
    andrej koelewijn Getting started with Nexus maven repository manager - http://www.andrejkoelewijn.com/wp... 15 hours ago from Google Reader - Comment - Like […]
    FriendFeed
  • @eelzinga Did you add the remote repository to the correct reposutory group in nexus? http://bit.ly/a2gTmZ March 9, 2010
    andrej koelewijn @eelzinga Did you add the remote repository to the correct reposutory group in nexus? http://www.andrejkoelewijn.com/wp... 15 hours ago from Twitter - Comment - Like […]
    FriendFeed
  • Getting started with Nexus maven repository manager March 9, 2010
    andrej koelewijn Getting started with Nexus maven repository manager - http://www.andrejkoelewijn.com/wp... 15 hours ago from Andrej Koelewijn - Comment - Like […]
    FriendFeed
  • Re: 7 Reasons For Apple To Acquire Adobe March 7, 2010
    andrej koelewijn Re: 7 Reasons For Apple To Acquire Adobe - http://thenextweb.com/apple... Saturday from Disqus - Comment - Like "Seems to me you left out the most important reason: ipad will be about media convergence: combining books with magazines, newspapers, tv and internet. Currently adobe seems to have the best tools to create these new types of […]
    FriendFeed
  • Do we need a new tech literacy? (Behind the “big data” services like Twitter, Facebook use) March 6, 2010
    andrej koelewijn Do we need a new tech literacy? (Behind the “big data” services like Twitter, Facebook use) - http://scobleizer.com/2010... Saturday from Google Reader - Comment - Like […]
    FriendFeed
  • Pivot: Business intelligence meets the internet as a database http://bit.ly/bFc6wp March 6, 2010
    andrej koelewijn Pivot: Business intelligence meets the internet as a database http://infosthetics.com/archive... Saturday from Twitter - Comment - Like […]
    FriendFeed
  • Live Labs Pivot: A Massive Interactive Zoom on Data (TED Talk) March 6, 2010
    andrej koelewijn Live Labs Pivot: A Massive Interactive Zoom on Data (TED Talk) - http://infosthetics.com/archive... Saturday from Google Reader - Comment - Like […]
    FriendFeed
  • http://twitpic.com/16ykk7 - March 6, 2010
    andrej koelewijn http://twitpic.com/16ykk7 - Saturday from Twitter - Comment - Like […]
    FriendFeed
  • http://twitpic.com/16y8ve - #vuurtoren #scheveningen March 6, 2010
    andrej koelewijn http://twitpic.com/16y8ve - #vuurtoren #scheveningen Saturday from Twitter - Comment - Like […]
    FriendFeed


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