Aug 13, 2015

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!

Adapter Registration

Now that we have an adapter we need to register it as a Spring bean to make it available in BootStrap.groovy.

Marshaller Registration

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.

The new GSONFAC has the registerObjectMarshaller method like JSON and 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!

About the Author

Patrick Double profile.

Patrick Double

Principal Technologist

I have been coding since 6th grade, circa 1986, professionally (i.e. college graduate) since 1998 when I graduated from the University of Nebraska-Lincoln. Most of my career has been in web applications using JEE. I work the entire stack from user interface to database.   I especially like solving application security and high availability problems.

One thought on “Sharing Grails HAL and JSON Renderers

Leave a Reply

Your email address will not be published.

Related Blog Posts
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]