【发布时间】:2015-04-04 00:48:45
【问题描述】:
我正在尝试将 Spring-data-rest 与 spring-data-mongodb 一起使用来公开只读资源。
我遇到的问题是我想对我的文档有不同的看法。 假设我在文档中有一些私人信息,我不想公开它们。
所以我尝试了几种方法。 我读了这篇 https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring 的帖子,描述了如何使用 JsonView 来选择我们想要公开的字段。
我试过这样:
@RepositoryRestResource(collectionResourceRel = "recommandation", path = "recommandations")
interface RecommandationRepository extends MongoRepository<Recommendation, ObjectId> {
@Override
@JsonView(View.Public.class)
Iterable<Recommendation> findAll(Iterable<ObjectId> objectIds);
... // other find methods
}
它不起作用。然而,在 cmets 中说:https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#comment-1725671983 答案建议使用@Projections 但是@Projections 会产生这样的网址:“…/recommandations{?projection}” 这意味着投影只是一个选项,因此仍然会暴露整个对象。
这里描述了另一种方法https://github.com/spring-projects/spring-data-rest/wiki/Configuring-the-REST-URL-path 它建议对我们不想公开的字段使用 @RestResource(exported = false) 注释。
但它不灵活。如果我想公开一个公共的只读 API 和一个私有的完全访问 API。不能按 api 禁用此注解。
还有其他建议吗?
【问题讨论】:
-
如何区分公共 API 和私有 API?同一个类有两个存储库吗?那么两节课怎么样?
标签: java spring-data-mongodb spring-data-rest