Great new bits in Grails 2.3.0

Kotek and I got to head out to SpringOne 2GX to speak, but we had to leave Todd, Yo, and Stroz at home. (Marc’s off in Pythonland.)

I’m in a session that is a) really good but b) stuff I know, so I skimming the newly released Grails 2.3.0 docs to identify stuff they might want to know about. It’s not a formal writeup, just notes.

Things I dig in 2.3.0:

h1. Data binding

We’ve recently hit some places where we’d like more control over binding request parameters to models. Grails 2.3.0 delivers.

h2. BindUsing

BindUsing will let you control what happens when you bind String request (form/url/json) data to a model/command:

@BindUsing - http://grails.org/doc/2.3.x/guide/theWebLayer.html#dataBinding

import org.grails.databinding.BindUsing
class Person {

String middleInitial

$BindUsing({ person, data ->
String candidate = data[‘middleInitial’]

if ( candidate ) {
return candidate.substring(0, 1).toUpperCase()
}
})
}

h2. Custom data conversion

Custom data converters are also worth checking out: their docs immediately follow the data binding bits. Basically, they let you say “any time you trying to bind a string from the request to a property of a given type, invoke this thing and use the result.”

h1. REST

The documentation at http://grails.org/doc/2.3.x/guide/single.html#REST probably answers questions I haven’t tried to sum up below. I had a number of them, read it, and I’m satisfied this stuff is good to go.

Given:

import grails.rest.*
@Resource(uri='/books')
class Book {

String title

static constraints = {
title blank:false
}
}

This:

http://localhost:8080/myapp/books/1.json

Yields:

{"id":1,"title":"The Stand"}

h2. Formats?

You can specify XML, JSON,

h3. Versioning?

Yep, supported, namespace-based.

h3. Security and doing it for ‘real’?

Yeah, that @Resource bit looks a bit scaffoldy and suspect to me. I don’t want a direct-pipe data access in my controller. I need some logic around it, and that belongs in a service.

Answer:

Extend RestfulController and override the appropriate action. Not -quite- sure what to return: I assume models. I’ll introspect the base class the first time I do it.

From the docs:

GET /books index
GET /books/create create
POST /books save
GET /books/${id} show
GET /books/${id}/edit edit
PUT /books/${id} update
DELETE /books/${id} delete

h3. But I need even more control…

…then you can bypass subclassing RestfulController and follow the docs to manually build your own, from scratch.

h3. But I don’t want to expose all properties of a model…

…then keep reading past the REST documentation and into Customizing the Default Renderers and Custom Object Marshallers.

h3. HAL, ATOM, and SOAP

HAL is a JSONish HATEOAS-oriented way of representing state (data, info, whatever) in REST. Grails supports it. The documentation and examples in “8.1.7.1 HAL Support” did more to tell me what it really is than articles about it.

ATOM is similar, but XML. Ew.

SOAP…f— SOAP.

h1. Scaffolding is a plugin

Pretty much says it all. Scaffolding is booted out of the core and is a plugin.

h1. Async programming

Docs at http://grails.org/doc/2.3.x/guide/single.html#async.

I use GPars in a few production applications right now, so I dig this stuff. You may want to read up on Promises before you dive in.

I was going to try to sum up the documentation, but it’s probably better just to read it.

Other bits worth noting:

  1. @DelegateAsync is badass.
  2. Each promise runs in a different thread, and each thread has its own Hibernate session!

h2. Async request processing

This’ll let you handle long-running requests (think a very long service method) without occupying a thread while they run. It’s a little mind-bending but really simple to do. Check the docs.

Joe Rinehart's been developing software for Web, mobile, and desktop since 1998. While he mainly now works in Java, Grails, and HTML5, he has a long history of community involvement in the Flash, Flex, and ColdFusion space. As a published author and award-winning speaker, he's bringing his skills to CompileDammit to help users new to Grails and Java. When he's not coding, he's either spending time with his family or feeding an appetite for endurance mountain bike racing. (8/9/2012 update: He's now either spending time with his family or getting over a huge knee injury caused by endurance mountain bike racing.)

Posted in Grails for CFML Developers, Groovy/Grails
2 comments on “Great new bits in Grails 2.3.0
  1. Marc Esher says:

    Joe, I saw on twitter a bit back that they were working on fixing the integration test problem — i.e. way too slow to run them b/c of spinning up the entire app. Any news on that?

  2. Joe Rinehart says:

    There’s a bit about how it now forks tests/builds/run-app to different JVMs. Graeme showed it doing unit tests, but not integration. I’ve got gvm now, so I can switch easily: I’ll take a peek myself in a while.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>