RESTing with JSON in Gaelyk
Posted by ice09 on February 1, 2010
Note: The complete sourcecode is available on github here (the subproject is ‘gaeloc’). For just downloading, unzipping and importing the project into Eclipse, press on the github site.
Doing REST is getting easier these days, however, there are still substantial differences between ease of usage between different REST frameworks.
A simple, concise approach is illustrated by the new REST support in Spring 3 as described in the previous post about this topic.
However, it can be even simpler. For some Android REST client, I wanted to choose the most intuitive and rapid way to implement a REST server prototype. Once again, I came up with Gaelyk, which I introduced here.
Technologies
- Gaelyk, a lightweight Groovy toolkit for the Google App Engine (Java)
- Groovy, namely the embeddable groovy-all-jar
- Jackson, an intuitive, nice & simple JSON processor
- the new Groovy Eclipse plugin
Setup
Currently, the best information about setup, installation and usage of Gaelyk is this post on developerworks.
I just updated the version numbers and changed the injected Google App Engine SDK element names (compare the version history). You will have to do the same if you want to get the downloadable post-project to work with Gaelyk 0.3.2.
The environment is a Eclipse 3.5.1 with the Google Plugin for Eclipse Plugin installed.
- Create new Web Application Project
- Add the Google App Engine SDK (for this project, GWT is not used)
- Add the Gaelyk and Groovy Jars
- Add the Jackson dependencies
- Add Groovy nature to the project to enable debugging
- For debugging, add the
WEB-INF/groovy
folder to the build path
Configuration
The important parts of the configuration are mentioned in the developerworks post. Just follow the instructions.
Afterwards, the routes for Gaelyk will be enabled and defined:
Changes to web.xml
<filter> <filter-name>RoutesFilter</filter-name> <filter-class>groovyx.gaelyk.routes.RoutesFilter</filter-class> </filter> <filter-mapping> <filter-name>RoutesFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The routes.groovy
file in WEB-INF
post "/new-user", forward: "/WEB-INF/groovy/post.groovy" get "/user/@name", forward: "/WEB-INF/groovy/get.groovy?name=@name"
The post.groovy
file called by POST on /new-user
import com.google.appengine.api.datastore.Entity import java.text.SimpleDateFormat import org.codehaus.jackson.map.ObjectMapper def requestComplete = new StringBuilder(); while ((line = request.getReader().readLine()) != null) { requestComplete.append(line); } ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(requestComplete.toString(), Map.class); def user = new Entity("user") user.name = map.key user.save()
The get.groovy
file called by GET on /user/{ID}
import org.codehaus.jackson.map.ObjectMapper import com.google.appengine.api.datastore.KeyFactory if (params["id"]) { def id = Long.parseLong(params["id"]) try { def key = KeyFactory.createKey("user", id) def user = datastore.get(key) ObjectMapper mapper = new ObjectMapper(); response.contentType = "application/json" StringWriter sw = new StringWriter(); mapper.writeValue(out, user); } catch (Throwable t) { } } else { forward "index.html" }
Starting and using the Google App Engine
You can start the Google App Engine locally by choosing “Run As…/Web Application”.
It helps a lot to be able to browse the datastore – this is now possible locally as well by visiting http://localhost:8888/_ah/admin/datastore
rok said
your post.groovy could be groovier:
ObjectMapper mapper = new ObjectMapper()
String json = mapper.writeValueAsString(params)
Map result = mapper.readValue(json, Map.class)
def user = new Entity(“user”)
result.each {
user.(it.key) = it.value
}
user.save()
also your routes.groovy is wrong:
get “/user/@id”, forward: “/WEB-INF/groovy/get.groovy?id=@id”
Cheers,
rok
Ben said
If you are using Gaelyk with version >= 0.4 you can make good use of the plugin system. I wrote a JSON plugin for Gaelyk that’ll make your life much easier: http://bit.ly/cf0Cir.
Ben