我正在尝试使用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
}
它不起作用。 然而评论中说:https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#comment-1725671983 答案建议使用@Projections。 但是@Projections的结果是像这样的url:“… / recommandations {?projection} ” 这意味着投影仅仅是一个选项,因此完整对象仍然会被暴露。
这里描述了另一种方法 https://github.com/spring-projects/spring-data-rest/wiki/Configuring-the-REST-URL-path 它建议为我们不想公开的字段使用@RestResource(exported=false)注释。
但是这种方法不够灵活。 如果我想公开只读API和私有完全访问API,则无法按API禁用此注释。
是否有其他建议?