为什么摘录投影不会自动应用于Spring Data REST项目资源?

时间:2022-07-25 20:03:44

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();
}

And the service repository:

和服务存储库:

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

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?

for / users它运行正常,业务领域暴露了嵌套实体,但是当我调用/ users / 1- nothing时,也是所有自定义方法..似乎投影不涉及任何方法除了/ users任何想法?

1 个解决方案

#1


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.

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

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.

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

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

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

EDIT: 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.

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

Read more on this topic in the Spring Data REST reference documentation.

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

#1


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.

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

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.

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

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

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

EDIT: 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.

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

Read more on this topic in the Spring Data REST reference documentation.

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