Returning Grails Validation Errors for Ajax Calls

January 6, 2011

I’m using Grails to create a REST service, and its built-in JSON formatting makes receiving and returning objects easy. When I get validation errors, I’d like to return the errors in JSON in a format that’s easy to use by my client. With a web client, Grails (and most other validation frameworks) by default outputs a list of human-friendly error messages. Ideally, what I’d like to return from my REST service is a list of strings, where each string is one field error.

Incidentally, REST’s HTTP status codes make it easy to distinguish when I’m returning objects vs. error messages. If I return a 200 or 201 status code, it’s success, and my client should expect an object from my data model back. If I return a 500 status code, my client should expect a list of error strings back.

It took me a little poking around in javadocs and Grails source to figure it out, but here’s the syntax that will work:

response.status = 500
render myGormObject.errors.allErrors.collect {
} as JSON

Easy Static Asset Expiration in J2EE

October 16, 2009

Just ran into a defect that came up because QA’s web browser had cached an old version of a JS file. I was hoping to find an easy way to configure caching for static assets in my webapp. Turns out that there’s a J2EE cache-filter that does it quite easily. Just add it to your pom or lib directory, then add a few filters and filter-mappings to your web.xml, and you’re good to go!

I configured 1 hr caching for JS and CSS, so they’ll be refreshed by the time I do a QA build. You could also set up your build script to make your cache settings environment-specific, so prod caches longer than QA. You can also use this to set longer caches on things like images (I set ‘em to cache for 1 day), to increase performance.

Glassfish Session Timeout Config

October 9, 2009

To configure session timeout in an app’s config file, use the session-properties element in sun-web.xml.

To configure it in the Glassfish console, go to Configurations > config-name > Web Container > Session Properties.


Get every new post delivered to your Inbox.

Join 525 other followers