Conditionally providing an element as a reference for implementing HAL in Java

I am trying to implement the HAL standard for JSON in a JAX-RS service. My project consists of users containing many projects, containing many nodes, containing a lot of data and pointers to other nodes.

So, when they hit the endpoint, I would like to insert the objects one level deep and the link after that:

  • / user has user data and "_embedded" projects, but only these projects contain "_links" for nodes (and self)
  • / project / 1234 has "_embedded" nodes, but these _links nodes are for additional data.

Etc.

Jackson JSONFilters look closely, but I do not quite understand this. It is especially relevant that sometimes the property will be displayed in the _embedded collection, and sometimes in the _links using different methods.

Anyone try something like this?

There is HalBuilder, but it looks like it requires manual serialization, which I would like to avoid. Again, Jackson seems to be almost the same code as manual serialization.

+4
source share
2 answers

You will need to implement your custom Jackson serializer ( http://jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/map/JsonSerializer.html )

Take a look at the Spring Hateoas project. They implemented the HalJacksonModule jackson HalJacksonModule ( https://github.com/SpringSource/spring-hateoas/commit/61e73107c1213556c025dc8f68a8784daf089796 ) to enable HAL serialization in Jackson. I think you can use it or adapt it to your needs.

In addition, the Spring Data Rest project (http://www.springsource.org/ spring-data / rest) provides a way to export your JPA model to REST (using hate) using Spring Hateoas. You can look at the code for inspiration or just use the framework in your code. (Remember to register HalJacksonModule in ObjectMapper ).

+4
source

I found that the RestExpress library is quite useful and includes HAL support. The author has done all the work of creating serialization mechanisms and building links based on a simple configuration.

https://github.com/RestExpress/HyperExpress

https://github.com/RestExpress/HyperExpress/tree/master/hal

0
source

All Articles