java - Selectively expand associations in Spring Data Rest response -
i have standard spring data jpa , spring data rest setup which, correctly, returns associations links correct resources.
{ "id": 1, "version": 2, "date": "2011-11-22", "description": "xpto", "_links": { "self": { "href": "http://localhost:8000/api/domain/1" }, "otherdomain": { "href": "http://localhost:8000/api/domain/1/otherdomain" } } }
however in some requests have association "otherdomain" expanded (so client not have n+1 requests full data).
is possible configure spring data rest handle response in way?
the default responses have stay same make sure payloads put
requests symmetric ones get
s return. however, spring data rest introduces feature called projections (see jira ticket details) works follows:
you create dedicated interface , add properties want include in response:
public interface myprojection { string getmyproperty(); myrelatedobject getotherdomain(); }
you can either
- annotate interface using
@projection
, place in same package domain type or subpackage of it - or manually register projection using
repositoryrestconfiguration
, callprojectionconfiguration().addprojection(…)
manually (by extendingrepositoryrestmvcconfiguration
, overridingconfigurerepositoryrestconfiguration(…)
).
this cause resources exposed domain type accept projection
parameter (name configurable projectionconfiguration
) name of projection. if given, skip default rendering (which includes rendering links related entities instead of embedding them) , let jackson render proxy backing given interface.
an example of can found in spring restbucks project. see orderprojection
interface definition.
Comments
Post a Comment