如何将投影应用于Spring Data REST查询方法资源? [英] How do I apply a projection to a Spring Data REST query method resource?
问题描述
我正在使用Spring Data REST 2.1.4.RELEASE。
I'm using Spring Data REST 2.1.4.RELEASE.
我创建了
- 一个实体
预订
, - 其REST存储库(扩展
CrudRepository
)名为BookingRepository
- 和投影
BookingDetails
(注释为@Projection(name =details,types = Booking.class)
)用于返回部分爆炸的链接实体,例如资源
,活动
,申请人
等。
- an entity
Booking
, - its REST repository (extending
CrudRepository
) namedBookingRepository
- and a projection
BookingDetails
(annotated with@Projection(name="details", types = Booking.class)
) for returning some of its linked entities exploded, such asResource
,Activity
,Applicant
etc.
客户通过 ... / rest / bookings
获取所有预订,JSON响应包含链接的链接实体。如果它添加?projection = details
,则会展开并返回链接的实体。这很棒。
The client gets all bookings with .../rest/bookings
and the JSON response includes links for the linked entities. If it adds ?projection=details
then the linked entities are exploded and returned. And this is great.
现在我将自定义方法添加到存储库中:
Now I add this custom method to the repository:
List<Booking> findByApplicant(@Param("applicant") Person applicant);
当客户使用 ... / rest / bookings / search调用它时/ findByApplicant?applicant = 5
,似乎无法请求详细信息
投影。以下尝试将被忽略:
When the client invokes it with .../rest/bookings/search/findByApplicant?applicant=5
, there seem to be no way to request the details
projection. Following attempts are ignored:
- 将
& projection = details
添加到查询字符串 -
使方法始终返回
BookingDetails
:
List<BookingDetails> findByApplicant(@Param("applicant") Person applicant);
汇总,自定义搜索方法( findBy *
)永远不会返回投影。除非您使用 @RepositoryRestResource(excerptProjection = BookingDetails.class)
注释存储库,但这会导致一些问题,首先客户端必须始终使用相同的投影。我们如何允许用户使用 findBy *
方法进行投影?
Summarizing, custom search methods (findBy*
) never return a projection. Unless you annotate the repository with @RepositoryRestResource(excerptProjection = BookingDetails.class)
, but this leads to some problems, first of all the client has to always use the same projection. How can we allow the user to use projections also with findBy*
methods?
推荐答案
我使用Spring Data REST 2.2.1验证了这一点,所以请更新它。确保您的客户端实际发送所需的参数。在调试时,我发现了如果您没有显式引用URI,则cURL会删除查询参数。所以这个:
I verfied this working with Spring Data REST 2.2.1, so please update it. Make sure your client actually sends the the requested parameters as you intend. While debugging, I found out that e.g. cURL drops query parameters if you do not explicitly quote the URI. So this:
curl http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details
将不发送任何查询参数。一旦引用URI,它就会。
will not send any of the query parameters. Once you quote the URI, it will.
curl 'http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details'
对于日益流行的 HTTPie 。有了它,所需的语法是:
Sort of the same is in place for the increasingly popular HTTPie. With it the required syntax is:
http :8080/orders/search/findByApplicant applicant==5 projection==details
如果你无法以这种方式工作,那么获得一个正在运行的示例项目会很酷看看。
In case you can't get it to work that way, it would be cool to get a running example project to look at.
这篇关于如何将投影应用于Spring Data REST查询方法资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!