Google App Engine for Java: CRUD Operations with JDO and Spring MVC
How to do CRUD operations using Google App Engine for Java and the Spring MVC framework.
In a prior post, I provided and introduction to GAE for Java including getting your local development environment setup and uploading your application to Google's servers.
For this post, I will outline how to do simple CRUD operations using GAE for Java and the latest version of Spring MVC (which is 3.0.0.M3). Combining GAE for Java and the Spring MVC framework makes for a powerful way of making your application both flexible and scalable.
Datastore Options with Google App Engine for Java
The basis for just about any web application generally includes a data store for storing information. Instead of a relational database like MySQL or Oracle, Google uses something called BigTable. For an explanation about how BigTable differs from a traditional relational database, you should check out "The Softer Side of Schemas" video from the Google I/O 2009 conference. With respect to GAE for java, you have essentially three different ways to interact with BigTable:
- JPA - Java Peristence API
- JDO - Java Data Objects
- Low level API - provided by Google for doing stuff that you can't otherwise do with JPA or JDO. Note that both JPA and JDO use this low-level API under the covers.
Development with Google App Engine for Java
To make development with GAE for Java, the folks at Google provide a couple of different ways to compile, package, and deploy your application. For the two mainstream Java IDE's, IntelliJ IDEA and Eclipse, there are plug-ins available. If you don't have one of these IDE's or prefer to work from the command line, there's Ant scripts available. My IDE of choice is IntelliJ IDEA, and the plug-in available is actually actively developed by JetBrains, makers of IDEA.
Getting Started with the View
Below is a simple web form for collecting information on API's:
isELIgnored="false" attribute in the page directive. I found that this is essential when using JSTL expressions with GAE. Otherwise, you'll get a nasty exception.
A Simple POJO with JDO Annotations
Spring 3 MVC Controller Using Annotations
Below is the ApiController.java class with an ApiService injected via Spring. It uses the nice REST url mappings with variable substitution introduced to the framework as of version 3:
Below is a custom class I introduced called GoogleDatastoreKeyEditor. This helps with being able to bind complex objects from a web form. Here I use some static methods KeyFactory.stringToKey(text) and KeyFactory.keyToString(value) to easily go back and forth between string and object representations of a Key.
Getting Interesting: ApiServiceImpl.java
Here's where all the actual work is done by providing methods that take care of the CRUD operations:
So that's a whirlwind tour of how to do some simple CRUD options with GAE for Java and Spring MVC. I'm in the process of figuring out to do things like many-to-many relationships and indexes. Look for that in my next post.