0

There are following URLS - /users/2/profile , /users/2/userPosts

I need to concat output of both Spring Data REST results in server side and build single JSON from them and send on different URL /users/2/custom.

So, I am thinking to make 2 calls to SDR urls from Spring MVC, can we do this using RestTemplate and some JSON concat utility , here server and database is on same machine so RestTemplate will probably have localhost

An example will help

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
fortm
  • 4,066
  • 5
  • 49
  • 79

1 Answers1

2

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).

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • Thanks @Oliver, I have a question however If I add Excerpt projections to ProfileRespository and UserPostRepository , will I be still able to create/update new Profile and Post records using PUT/PATCH requests from these URL - /profiles and /userPosts – fortm Jan 07 '15 at 09:08
  • Second doubt is there as well, when I make a repository an Excerpt projection, I find that associations are indeed embedded in Entities. To give an example at one point of time I created UserRepository itself an Excerpt projection. And since UserPost and Profile have user object, they show User projetion data by default. Is there any way I can hide this projected data from coming userpost and profile. I tried @JsonIgnore on User but it did not help – fortm Jan 07 '15 at 09:14
  • 1
    No, excerpt projections are applied *everywhere* an association is embedded. I'd generally recommend to avoid bi-directional relationships in this scenario as all of your three concepts are basically aggregate roots (due to being managed by a repository) and it's harder to enforce aggregate consistency if bi-directional relationships are used. If you selectively want to apply the merge on the user side only, go with the first suggestion and craft a projection for `User` so that clients have dedicated control over when to use it or not. – Oliver Drotbohm Jan 07 '15 at 09:30
  • I implemented with Option 1 and am running into pagination Issue since it is embedding all data in Projection. Everything other than that is alright. I had figured out a solution before using SPring MVC URL forwarding and am clueless here. http://stackoverflow.com/questions/27539550/use-case-for-pagination-of-embedded-resources – fortm Jan 07 '15 at 15:39
  • I am just thinking on the lines of having Page getUserPosts(); in UserWithDetails – fortm Jan 07 '15 at 15:58