10.12.1 HAL Support

HAL is a standard exchange format commonly used when developing REST APIs that follow HATEOAS principals. An example HAL document representing a list of orders can be seen below:

  1. {
  2. "_links": {
  3. "self": { "href": "/orders" },
  4. "next": { "href": "/orders?page=2" },
  5. "find": {
  6. "href": "/orders{?id}",
  7. "templated": true
  8. },
  9. "admin": [{
  10. "href": "/admins/2",
  11. "title": "Fred"
  12. }, {
  13. "href": "/admins/5",
  14. "title": "Kate"
  15. }]
  16. },
  17. "currentlyProcessing": 14,
  18. "shippedToday": 20,
  19. "_embedded": {
  20. "order": [{
  21. "_links": {
  22. "self": { "href": "/orders/123" },
  23. "basket": { "href": "/baskets/98712" },
  24. "customer": { "href": "/customers/7809" }
  25. },
  26. "total": 30.00,
  27. "currency": "USD",
  28. "status": "shipped"
  29. }, {
  30. "_links": {
  31. "self": { "href": "/orders/124" },
  32. "basket": { "href": "/baskets/97213" },
  33. "customer": { "href": "/customers/12369" }
  34. },
  35. "total": 20.00,
  36. "currency": "USD",
  37. "status": "processing"
  38. }]
  39. }
  40. }

Exposing Resources Using HAL

To return HAL instead of regular JSON for a resource you can simply override the renderer in grails-app/conf/spring/resources.groovy with an instance of grails.rest.render.hal.HalJsonRenderer (or HalXmlRenderer for the XML variation):

  1. import grails.rest.render.hal.*
  2. beans = {
  3. halBookRenderer(HalJsonRenderer, rest.test.Book)
  4. }

You will also need to update the acceptable response formats for the resource so that the HAL format is included. Not doing so will result in a 406 - Not Acceptable response being returned from the server.

This can be done by setting the formats attribute of the Resource transformation:

  1. import grails.rest.*
  2. @Resource(uri='/books', formats=['json', 'xml', 'hal'])
  3. class Book {
  4. ...
  5. }

Or by updating the responseFormats in the controller:

  1. class BookController extends RestfulController {
  2. static responseFormats = ['json', 'xml', 'hal']
  3. // ...
  4. }

With the bean in place requesting the HAL content type will return HAL:

  1. $ curl -i -H "Accept: application/hal+json" http://localhost:8080/books/1
  2. HTTP/1.1 200 OK
  3. Server: Apache-Coyote/1.1
  4. Content-Type: application/hal+json;charset=ISO-8859-1
  5. {
  6. "_links": {
  7. "self": {
  8. "href": "http://localhost:8080/books/1",
  9. "hreflang": "en",
  10. "type": "application/hal+json"
  11. }
  12. },
  13. "title": "\"The Stand\""
  14. }

To use HAL XML format simply change the renderer:

  1. import grails.rest.render.hal.*
  2. beans = {
  3. halBookRenderer(HalXmlRenderer, rest.test.Book)
  4. }

Rendering Collections Using HAL

To return HAL instead of regular JSON for a list of resources you can simply override the renderer in grails-app/conf/spring/resources.groovy with an instance of grails.rest.render.hal.HalJsonCollectionRenderer:

  1. import grails.rest.render.hal.*
  2. beans = {
  3. halBookCollectionRenderer(HalJsonCollectionRenderer, rest.test.Book)
  4. }

With the bean in place requesting the HAL content type will return HAL:

  1. $ curl -i -H "Accept: application/hal+json" http://localhost:8080/books
  2. HTTP/1.1 200 OK
  3. Server: Apache-Coyote/1.1
  4. Content-Type: application/hal+json;charset=UTF-8
  5. Transfer-Encoding: chunked
  6. Date: Thu, 17 Oct 2013 02:34:14 GMT
  7. {
  8. "_links": {
  9. "self": {
  10. "href": "http://localhost:8080/books",
  11. "hreflang": "en",
  12. "type": "application/hal+json"
  13. }
  14. },
  15. "_embedded": {
  16. "book": [
  17. {
  18. "_links": {
  19. "self": {
  20. "href": "http://localhost:8080/books/1",
  21. "hreflang": "en",
  22. "type": "application/hal+json"
  23. }
  24. },
  25. "title": "The Stand"
  26. },
  27. {
  28. "_links": {
  29. "self": {
  30. "href": "http://localhost:8080/books/2",
  31. "hreflang": "en",
  32. "type": "application/hal+json"
  33. }
  34. },
  35. "title": "Infinite Jest"
  36. },
  37. {
  38. "_links": {
  39. "self": {
  40. "href": "http://localhost:8080/books/3",
  41. "hreflang": "en",
  42. "type": "application/hal+json"
  43. }
  44. },
  45. "title": "Walden"
  46. }
  47. ]
  48. }
  49. }

Notice that the key associated with the list of Book objects in the rendered JSON is book which is derived from the type of objects in the collection, namely Book. In order to customize the value of this key assign a value to the collectionName property on the HalJsonCollectionRenderer bean as shown below:

  1. import grails.rest.render.hal.*
  2. beans = {
  3. halBookCollectionRenderer(HalCollectionJsonRenderer, rest.test.Book) {
  4. collectionName = 'publications'
  5. }
  6. }

With that in place the rendered HAL will look like the following:

  1. $ curl -i -H "Accept: application/hal+json" http://localhost:8080/books
  2. HTTP/1.1 200 OK
  3. Server: Apache-Coyote/1.1
  4. Content-Type: application/hal+json;charset=UTF-8
  5. Transfer-Encoding: chunked
  6. Date: Thu, 17 Oct 2013 02:34:14 GMT
  7. {
  8. "_links": {
  9. "self": {
  10. "href": "http://localhost:8080/books",
  11. "hreflang": "en",
  12. "type": "application/hal+json"
  13. }
  14. },
  15. "_embedded": {
  16. "publications": [
  17. {
  18. "_links": {
  19. "self": {
  20. "href": "http://localhost:8080/books/1",
  21. "hreflang": "en",
  22. "type": "application/hal+json"
  23. }
  24. },
  25. "title": "The Stand"
  26. },
  27. {
  28. "_links": {
  29. "self": {
  30. "href": "http://localhost:8080/books/2",
  31. "hreflang": "en",
  32. "type": "application/hal+json"
  33. }
  34. },
  35. "title": "Infinite Jest"
  36. },
  37. {
  38. "_links": {
  39. "self": {
  40. "href": "http://localhost:8080/books/3",
  41. "hreflang": "en",
  42. "type": "application/hal+json"
  43. }
  44. },
  45. "title": "Walden"
  46. }
  47. ]
  48. }
  49. }

Using Custom Media / Mime Types

If you wish to use a custom Mime Type then you first need to declare the Mime Types in grails-app/conf/application.groovy:

  1. grails.mime.types = [
  2. all: "*/*",
  3. book: "application/vnd.books.org.book+json",
  4. bookList: "application/vnd.books.org.booklist+json",
  5. ...
  6. ]
It is critical that place your new mime types after the 'all' Mime Type because if the Content Type of the request cannot be established then the first entry in the map is used for the response. If you have your new Mime Type at the top then Grails will always try and send back your new Mime Type if the requested Mime Type cannot be established.

Then override the renderer to return HAL using the custom Mime Types:

  1. import grails.rest.render.hal.*
  2. import grails.web.mime.*
  3. beans = {
  4. halBookRenderer(HalJsonRenderer, rest.test.Book, new MimeType("application/vnd.books.org.book+json", [v:"1.0"]))
  5. halBookListRenderer(HalJsonCollectionRenderer, rest.test.Book, new MimeType("application/vnd.books.org.booklist+json", [v:"1.0"]))
  6. }

In the above example the first bean defines a HAL renderer for a single book instance that returns a Mime Type of application/vnd.books.org.book+json. The second bean defines the Mime Type used to render a collection of books (in this case application/vnd.books.org.booklist+json).

application/vnd.books.org.booklist+json is an example of a media-range (http://www.w3.org/Protocols/rfc2616/rfc2616.html - Header Field Definitions). This example uses entity (book) and operation (list) to form the media-range values but in reality, it may not be necessary to create a separate Mime type for each operation. Further, it may not be necessary to create Mime types at the entity level. See the section on "Versioning REST resources" for further information about how to define your own Mime types.

With this in place issuing a request for the new Mime Type returns the necessary HAL:

  1. $ curl -i -H "Accept: application/vnd.books.org.book+json" http://localhost:8080/books/1
  2. HTTP/1.1 200 OK
  3. Server: Apache-Coyote/1.1
  4. Content-Type: application/vnd.books.org.book+json;charset=ISO-8859-1
  5. {
  6. "_links": {
  7. "self": {
  8. "href": "http://localhost:8080/books/1",
  9. "hreflang": "en",
  10. "type": "application/vnd.books.org.book+json"
  11. }
  12. },
  13. "title": "\"The Stand\""
  14. }

An important aspect of HATEOAS is the usage of links that describe the transitions the client can use to interact with the REST API. By default the HalJsonRenderer will automatically create links for you for associations and to the resource itself (using the "self" relationship).

However you can customize link rendering using the link method that is added to all domain classes annotated with grails.rest.Resource or any class annotated with grails.rest.Linkable. For example, the show action can be modified as follows to provide a new link in the resulting output:

  1. def show(Book book) {
  2. book.link rel:'publisher', href: g.createLink(absolute: true, resource:"publisher", params:[bookId: book.id])
  3. respond book
  4. }

Which will result in output such as:

  1. {
  2. "_links": {
  3. "self": {
  4. "href": "http://localhost:8080/books/1",
  5. "hreflang": "en",
  6. "type": "application/vnd.books.org.book+json"
  7. }
  8. "publisher": {
  9. "href": "http://localhost:8080/books/1/publisher",
  10. "hreflang": "en"
  11. }
  12. },
  13. "title": "\"The Stand\""
  14. }

The link method can be passed named arguments that match the properties of the grails.rest.Link class.