Quick primer:
For more information regarding marshaling, consult your local wikipedia page on marshaling, but for our purposes, this is all you need to know.
- Marshalling: Object transformed into simple format (JSON for this example)
- Unmarshalling: simple format to Object
Prerequisties
In order to run this example, you need- Grails (I'm using 2.2.1)
- Postman - for posting JSON to the Grails app
Example
1. Setup a new project, for this example I will use "jason".- grails create-app jason
2. Create a new domain object, for our example I will use
package example3. Scaffold the Book artifacts
class Book {
String author
String title
}
grails generate-all example.Book4. Edit the Conf/UrlMappings.groovy. For now we will just hardcode the url mappings for book, so add the following to the top of the mappings block, such that your UrlMappings.groovy looks like:
static mappings = {5. Make a few small edits to the BookController, adding the Grails JSON converter and editing save() , list(), and show() methods to return the persisted objects as JSON.
"/book/"(controller: "book", parseRequest:true){
action = [GET:'list', POST:'save']
}
"/book/$id?"(controller: "book", parseRequest:true){
action = [GET:'show'] // could also make this action = [GET:'show', PUT:'update', DELETE:'delete'], but you would need to modify those methods slightly
}
"/$controller/$action?/$id?"{
...
import grails.converters.JSON;
...
def save() {
def bookInstance = new Book(params)
if (!bookInstance.save(flush: true)) {
render(view: "create", model: [bookInstance: bookInstance])
return
}
render bookInstance as JSON
}
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
render Book.list(params) as JSON;
}
def show(Long id) {
def bookInstance = Book.get(id)
if (!bookInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), id])
redirect(action: "list")
return
}
render bookInstance as JSON;
}
7. Post to the application
{"title": "The Definitive Guide to Grails", "author": "Graeme Rocher"}
9. "Get" a Book by id from the application. Because we mapped the GET action to 'show' in the UrlMappings, we should get the Book by id.
11. You can also check that this is working using the in the Grails dbconsole
- Open Postman, enter URL request here: http://localhost:8080/jason/book
- Select "Post", since we are adding a new Book object
- Select Header button
- Header: Content-Type
- Value: application/json or text/json
- Select "raw" from the parameter options (form-data, x-www-form..., raw)
- Enter the following in the textbox provided
{"title": "Programming Grails", "author": "Burt Beckwith"} - Click Send
- You should receive a response like:
{
"class": "example.Book",
"id": 1,
"author": "Burt Beckwith",
"title": "Programming Grails"
}
{"title": "The Definitive Guide to Grails", "author": "Graeme Rocher"}
9. "Get" a Book by id from the application. Because we mapped the GET action to 'show' in the UrlMappings, we should get the Book by id.
- Open Postman, enter URL request here: http://localhost:8080/jason/book/1
- Alternatively, you could make the url http://localhost:8080/jason/book and add a URL param id=1
- Select "Get", since we are retrieving an existing Book object
- Select Header button
- Header: Content-Type
- Value: application/json or text/json
- Click Send
- You should receive a response like:
{
"class": "example.Book",
"id": 1,
"author": "Burt Beckwith",
"title": "Programming Grails"
}
10. "Get" all Books from the application. Because we mapped the GET action to "list" in the UrlMappings, we should get all the Books in the application.
- Open Postman, enter URL request here: http://localhost:8080/jason/book/
- Select "Get", since we are retrieving an existing Book object
- Select Header button
- Header: Content-Type
- Value: application/json or text/json
- Click Send
- You should receive a response like:
[
{
"class": "example.Book",
"id": 1,
"author": "Burt Beckwith",
"title": "Programming Grails"
},
{
"class": "example.Book",
"id": 2,
"author": "Graeme Rocher",
"title": "The Definitive Guide to Grails"
}
]
- http://localhost:8080/jason/dbconsole
- By default, the JDBC URL is jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000, but if you changed it in conf/datasource.groovy, change this value accordingly