Using custom media type (not HAL)

40 views Asked by At

With Spring Data REST would it be possible to use a custom media type ? (meaning for example JSON:API / media type application/vnd.api+json rather than HAL and media type application/hal+json).

The documentation says - in several places (example) - that only media types application/hal+json and application/json are supported.

However, as far as I understand, Spring Data REST is build on top of Spring HATEOAS which does indeed support custom media types, so I'm thinking it should be possible.

I want to leverage the convenience of Spring Data REST in that I don't want to create my own controller classes. I like the fact that Spring Data REST does all that for me and I simply want to expose a Spring Data CRUD repository using a sane REST convention.

1

There are 1 answers

0
peterh On

Based on this answer from VMWare developer it seems the topic has been debated and here's the reasoning:

I wish it were that simple. Most people look at Spring Data REST and like it because they easily get HTTP resources for their repositories. The primary feature of it though is not the actual exposure of the resources but the derivation of the representation boundaries based on the aggregate structure expressed in the domain model. This includes using HAL's embedding mechanism, applying projections to those values embedded. Properly handling the embedded values if a HAL representation is posted back to the servers.

Without proper testing, we cannot make sure that simply letting users activate a new media type (Collection JSON, Siren etc.) would result in them seeing useful, correct output that lets all exposed HTTP resources work as advertised. I am not even saying it wouldn't work for some media types that are different flavors of "presenting data". I just can't open a door ("You can now simply activate new media types") and then risk having to run after folks showing left and right reporting that "this doesn't work with Siren", "that doesn't work with Collection JSON" and "Oh, this doesn't work with my customly implemented media type.".

For some media types that include forms, this wouldn't work by definition, as our controllers currently don't add any affordances at all. I can imagine proceeding with that to see how that'd work out with HAL FORMS, add tests about the now expected representation and at some point adding yet another media type and see what that results in. That said, that's not a half-a-day effort.

Bottom line: not possible.