春季数据休息 - 不允许返回实体,而只能视图(预测)
我的目的是确保客户端无法通过Spring Data REST自动暴露的API直接访问(检索)实体,而仅对这些实体的视图(JPA的预测)。
到目前为止代码>在存储库上的注释。
如何配置弹簧数据休息,以使其对检索特定实体的端点也相同?例如/api/v1/customers/1
My objective is to make sure that a client can't access (retrieve) directly an entity through the Spring Data REST auto-exposed APIs, but rather only to the views (JPA's projections) of those entities.
So far I've managed to achieve it only for the APIs that return a collection of entities (such as findAll()
) by using the @RepositoryRestResource(excerptProjection = CustomerView.class)
annotation on the repository.
How to configure Spring Data REST so that it does the same also for endpoints that retrieve a specific entity? such as /api/v1/customers/1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参见为什么摘录投影不适用于弹簧数据休息项目资源?
如果要将投影应用于特定实体(即项目资源),
设置URI模板变量
投影
构建URL路径/api/v1/customers/1?propp者,propptions = customerview
。名称customerview
是注释中设置的@projection
。请参阅doc https:/// docs.spring.io/spring-data/rest/docs/current/referent/html/#projections-excerpts.projections用宏澄清后编辑:
宏想隐藏一些源性字段,例如密码。然后,应将杰克逊注释
@jsonignore
添加到senditive字段中,以将其隐藏在响应JSON中。See Why is an excerpt projection not applied automatically for a Spring Data REST item resource?
If you want to apply projection to a specific entity (that is, item resource),
set the uri template variable
projection
to construct a url path/api/v1/customers/1?projection=customerView
. The namecustomerView
is what is set in the annotation@Projection
. see the doc https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projectionsEdit after clarify with Macro:
Macro wants to hide some sentitive fields such as password. Then the jackson annotation
@JsonIgnore
should be added to the sentitive fields to hide them from response json.