Sharing Grails HAL and JSON Renderers
Grails provides nice features for creating web services, customization is usually terse. According to the “Registering Custom Objects Marshallers” section of the Grails User Guide, custom object marshallers can registered using a simple closure. However, it turns out that these marshallers do not apply if you are serving HAL (HATEOS) in addition to JSON/XML. This post will detail a method to share marshallers across XML, JSON and HAL flavors of web services.
Create Adapter for Gson
The problem happens because the HAL renderer is using Gson for marshalling which does fit the following pattern in the Grails User Guide:
In order to include HAL in this list, we need to create an adapter class to register the closure. The adapter will then work the closure into the
Gson library. Note that
Gson performs input and output processing, the approach here only addresses output processing.
We need a test, right? Of course we do!
Now that we have an adapter we need to register it as a Spring bean to make it available in
Now for the part for which we’ve been waiting. These are only examples and not necessary to make this work for your own marshallers.
GSONFAC has the
registerObjectMarshaller method like
XML. Using the Groovy splat operator, we can easily re-use the closure.
This was a difficult find for me, why the JSON tests were working but HAL was not. Hopefully this post will save you time and write less code!
One thought on “Sharing Grails HAL and JSON Renderers”