You might rather want to look into the projections feature of Spring Data REST which allows you to craft custom responses by using interfaces as described in this blog post.
As both properties (profile
and userPosts
) seem to be associations of a user item resource it should be sufficient to do something like this:
@Projection(types = User.class)
interface UserWithDetails {
// List getters for basic properties you want to expose…
// Add dedicated getters for associations to be included
Profile getProfile();
List<Post> getUserPosts();
}
Clients can now pass a projection
parameter to the resources exposed to see the expanded representation.
As an alternative, you can create these kinds of interfaces for Profile
and Post
and configure @RepositoryRestResource
on the repositories for both of these types to contain excerptProjection = YourProjectionInterface.class
. This will cause the projections to be rendered whenever an association is included in the response (i.e. an actually-linked-to-resource could be embedded).