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

查看:142
本文介绍了为什么摘录投影不会自动应用于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-时,什么也没有,所有自定义方法也是如此. c0> 有任何想法吗?

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天全站免登陆