春季数据休息 - 不允许返回实体,而只能视图(预测)

发布于 2025-01-22 22:28:49 字数 174 浏览 0 评论 0原文

我的目的是确保客户端无法通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-01-29 22:28:49

参见为什么摘录投影不适用于弹簧数据休息项目资源?

如果要将投影应用于特定实体(即项目资源),
设置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 name customerView is what is set in the annotation @Projection. see the doc https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections

Edit 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文