为什么不为 Spring Data REST 项目资源自动应用摘录投影? [英] Why is an excerpt projection not applied automatically for a Spring Data REST item resource?

查看:29
本文介绍了为什么不为 Spring Data REST 项目资源自动应用摘录投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个应该暴露嵌套实体的投影:

I made a projection which should expose nested entities:

@Projection(name = "inlineBusiness", types = { UserModel.class })
public interface InlineBusinessUserModelProjection {

    String getUsername();

    String getFirstName();

    String getLastName();

    Date getBirthdate();

    String getEmail();

    BusinessModel getBusiness();
}

和服务存储库:

@RepositoryRestResource(collectionResourceRel = "users", path = "users",
       excerptProjection = InlineBusinessUserModelProjection.class)
public interface UserRepository extends BaseDAO<UserModel> {..}

对于 /users 它工作正常,业务字段通过嵌套实体公开,但是当我调用 /users/1 时 - 什么都没有,还有所有自定义方法.. 似乎除了 /users 之外的任何方法都没有涉及投影有任何想法吗?

for /users it works fine, the business field is exposed with nested entity, but when I call /users/1- nothing, also all the custom methods.. Seems like projection isn't involved on any methods except of /users Any ideas?

推荐答案

按设计工作.只要在 _embedded 子句中使用目标类型的实例(在您的情况下为 UserModel),就会使用摘录投影.因此,摘录是在资源本身未呈现但指向的任何地方使用的某种预览.这通常是来自馆藏资源或协会的情况.

That works as designed. An excerpt projection is used whenever an instance of the target type (UserModel in your case) is used within in an _embedded clause. Thus the excerpt is some kind of preview used everywhere the resource itself is not rendered but pointed to. This is usually the case from collection resources or for associations.

默认情况下在项目资源上使用摘录投影从另一个角度来看并没有真正意义:摘录投影是某些域对象的只读视图.如果默认情况下返回项目资源的该视图,客户端如何知道它必须发送哪些数据来更新资源.为摘录投影创建的 JSON 文档不能简单地获取、修改并用于发送 PUT 请求以更新资源 - 根据定义.

Using an excerpt projection by default on an item resource doesn't really make sense from another point of view: excerpt projections are a read-only view on some domain object. If you return that view for an item resource by default, how would a client know which data it had to send to update the resource. A JSON document created for an excerpt projection, can't be simply taken, modified and used to send a PUT request to update the resource – by definition.

如果要将投影应用于项目资源,请使用投影名称填充 projection URI 模板变量.

If you want to apply a projection to the item resource, populate the projection URI template variable with the name of the projection.

如果手动选择投影未应用,请确保 InlineBusinessUserModelProjection 实际上已注册用于一般用途.确保类型位于 UserModel 的相同包或子包中. 或者通过 RepositoryRestConfiguration.projectionConfiguration().addProjection(...).手动配置使得在投影类型上使用 @Projection 已过时.

In case the projections don't get applied if you manually select them make sure InlineBusinessUserModelProjection is actually registered for general use. Be sure the type is located in the very same package or sub-package of UserModel. Alternatively manually register the projection via RepositoryRestConfiguration.projectionConfiguration().addProjection(…). Manual configuration makes the use of @Projection on the projection type obsolete.

Spring Data REST 参考文档中阅读有关此主题的更多信息.

这篇关于为什么不为 Spring Data REST 项目资源自动应用摘录投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆