rest - Disable Hypertext Application Language (HAL) in JSON? -
using spring data rest jpa in version 2.0.2.release.
how can disable hypertext application language (hal) in json ? http://stateless.co/hal_specification.html
i have tried many things already, no avail. example, have set accept , content-type headers "application/json" instead of "application/hal+json" still receive json content hyper links.
for example, i'd like:
{ "name" : "foo", "street" : "street bar", "streetnumber" : 2, "streetletter" : "b", "postcode" : "d-1253", "town" : "munchen", "country" : "germany", "phone" : "+34 4410122000", "vat" : "000000001", "employees" : 225, "sector" : { "description" : "marketing", "average profit": 545656665, "average employees": 75, "average profit per employee": 4556 } }
instead of:
{ "name" : "foo", "street" : "street bar", "streetnumber" : 2, "streetletter" : "b", "postcode" : "d-1253", "town" : "munchen", "country" : "germany", "phone" : "+34 4410122000", "vat" : "000000001", "employees" : 225, "_links" : { "self" : { "href" : "http://localhost:8080/app/companies/1" }, "sector" : { "href" : "http://localhost:8080/app/companies/1/sector" } } }
thanks help.
(hyper)media types
the default settings spring data rest use hal default hypermedia representation format, server return following given accept
headers:
- no header ->
application/hal+json
-> hal application/hal+json
->application/hal+json
-> halapplication/json
->application/json
-> hal (this default configures)application/x-spring-data-verbose+json
->application/x-spring-data-verbose+json
-> spring data specific format (usinglinks
links container ,content
wrapper collection items.
if configure repositoryrestconfiguration.setdefaultmediatype(…)
non-hal format, server return spring data specific json format unless explicitly ask application/hal+json
. admittedly configuration option bit misleading, filed datarest-294 improve this. issue resolved in 2.1 rc1 (dijkstra) 2014.
note need hypermedia format in place able express relations between managed resources , enable discoverability of server. there's no way you'll able rid of completely. due fact crash server if expose entities have bidirectional relationships or make enormous object graph.
inlining related entities
if never want have sectors linked , inline them, 1 option exclude sectorrepository
being exported rest resource in first place. can achieve annotating repository interface @repositoryrestresource(exported = false)
.
to representation returned posted in lower example have @ projections feature introduced in spring data rest 2.1 m1. allow craft optional views on resource can differ default 1 via simple interface.
you'd define interface:
@projection(name = "foo", types = yourdomainclass.class) interface inlined { // list other properties sector getsector(); }
if either put interface (sub)package of domain class or manually register via repositoryrestconfiguration.projectionconfiguration()
resources exposing yourdomainclass
accept request parameter projection
passing in foo
in example render inlined representation want it.
this commit has more info on feature in general, this commit has example projection defined.
Comments
Post a Comment