How to apply a projection to a resource resource query request method Spring Data REST?

I am using Spring Data REST 2.1.4.RELEASE.

I created

  • Booking object
  • its REST repository ( CrudRepository extension) named BookingRepository
  • and the BookingDetails projection (annotated using @Projection(name="details", types = Booking.class) ) to return some objects associated with it, such as Resource , Activity , Applicant , etc.

The client receives all orders with .../rest/bookings , and the JSON response includes links for related objects. If he adds ?projection=details , then related objects will be blown up and returned. And that's great.

Now I am adding this custom method to the repository:

 List<Booking> findByApplicant(@Param("applicant") Person applicant); 

When a client calls it with .../rest/bookings/search/findByApplicant?applicant=5 , there seems to be no way to request details projection. The following attempts are ignored:

  • adding &projection=details to the query string
  • so that the method always returns BookingDetails :

     List<BookingDetails> findByApplicant(@Param("applicant") Person applicant); 

To summarize, custom search methods ( findBy* ) never return a projection . If you do not annotate the repository using @RepositoryRestResource(excerptProjection = BookingDetails.class) , but this leads to some problems, the client should always use the same projection first. How can we allow the user to use forecasts also with findBy* methods?

+7
java spring rest spring-data-rest projection
source share
1 answer

I have confirmed that this works with Spring Data REST 2.2.1, so please update it. Make sure your client actually sends the requested parameters as you plan. During debugging, I found out that, for example, cURL overrides the request parameters if you do not explicitly specify a URI. So:

 curl http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details 

will not send any request parameters. Once you specify a URI, it will be.

 curl 'http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details' 

Regarding the same as for the more popular HTTPie . The syntax required is:

 http :8080/orders/search/findByApplicant applicant==5 projection==details 

In case you cannot get it to work this way, it would be great to get a sample project running.

+13
source share

All Articles