We recently launched a new Spring Cloud project on GitHub called NixMash Cloud. It is loosely based on NixMash Blog so as a Cloud Application it serves up REST data from various Service Modules like a Posts Module. A typical /posts display generated in Spring Data Rest application/hal+json would look like you'd expect.
But what if we wanted to make just Post Titles available? That's where Spring Projections come into play.
The @Projection Interface
We define our Projection in an Interface Class with the @Projection Annotation listing the fields we want to include. In the @Projection properties we assign a Projection Name and Entity.
To retrieve our Post Titles we add ?projection=postTitles to the url with the following result.
We could also add an excerptProjection=PostTitles.class property to our Repository @RepositoryRestResource Annotation which would make the Projection Global.
A Cross-Module Tip
In our NixMash Spring Cloud app, the Projection is defined in a CORE Module and called from our POSTS Service Module. For some reason the Projection will be ignored unless it is explicitly added in our POSTS Module Configuration.
Source Code Notes for this Post
Source code discussed in our Spring Cloud Series is found in NixMash Spring Cloud on GitHub. Code for this particular post is found in branch nixmashcloud-v0.1.0 – Initial Layout and Structure.