java - Spring Data REST How to add embedded resources inline -
i'm using spring data rest , hateoas in combination hal browser. works perfectly, make json dump of specific entity (a set of) associated objects. used @projection
got stuck again.
fyi: normal behaviour (with embedded , links etc) should remain besides new endpoint (without embedded , links).
to further illustrate problem/question:
class person { string name; list<company> companies; } class company { string name; address address; } class address { string street; }
now see this:
{ "name": "john", "companies": [ { "name": "stackoverflow", "address": {"street": "highway blvd."} }, { "name": "oracle", "address": {"street": "main rd."} } ] }
while i'm getting this:
{ "name": "john", "_links": { "self": {"href": "http...."}, "companies": {"href": "http ..."} }, }
see also: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
in example introduced 2 difficulties have: lists (companies) , multiple levels: person->company->address. both required work (probably 5 levels, of have 'many' relations).
the accepted method of inlining entities projections, identified. projections inlined, 1 option create projections each of entities , combine them so:
@projection(name = "personprojection", types = person.class) public interface personprojection { string getfirstname(); list<companyprojection> getcompanies(); } @projection(name = "companyprojection", types = company.class) public interface companyprojection { string getname(); addressprojection getaddress(); } @projection(name = "addressprojection", types = address.class) public interface addressprojection { string getstreet(); }
a get people/1?projection=personprojection
still render _links
elements, nesting want:
{ "companies" : [ { "address" : { "street" : "123 fake st", "_links" : { "self" : { "href" : "http://localhost:8080/addresses/1{?projection}", "templated" : true } } }, "name" : "acme inc.", "_links" : { "self" : { "href" : "http://localhost:8080/companies/1{?projection}", "templated" : true }, "address" : { "href" : "http://localhost:8080/companies/1/address" } } } ], "firstname" : "will", "_links" : { "self" : { "href" : "http://localhost:8080/people/1" }, "person" : { "href" : "http://localhost:8080/people/1{?projection}", "templated" : true }, "companies" : { "href" : "http://localhost:8080/people/1/companies" } } }
alternatively, if don't need expose company
, address
entities rest resources, can mark repositories @repositoryrestresource(exported=false)
, , inlined wherever referenced, without need projections.
a final caveat, though - request fighting against ethos of spring data rest , spring hateoas, , inviting big, unwieldy queries suffering n+1 problem. remember spring data rest not turnkey solution turning domain model api, , rendering deep object graphs (if intention) potentially might expose custom controller endpoint on ad-hoc basis can control conditions thoroughly.
Comments
Post a Comment