【发布时间】:2014-04-23 03:15:47
【问题描述】:
我想创建一个 Spring Data Repository,它应该通过 Spring Data Rest 公开。
问题是我的“实体”不是来自数据库。我有几个具有自定义注释并被扫描的类,这提供了一些我想向客户公开的信息。
所以基本上我需要类似的东西:
@RestRepository(path="myClasses")
interface MyRepo extends Repository<MyClass, String> {
List<MyClass> findAll();
}
class MyRepoImpl implements MyRepo {
List<MyClass> classes;
MyRepoImpl() {
// fetch stuff via classpath scanning
// and save it to "classes"
}
@Override
List<MyClass> findAll() {
return classes;
}
}
我现在从 Spring Data MongoDB 复制并粘贴了大约 10 个文件,以便使用自定义 FactoryBean 等获得自定义 @EnableCustomRepositories 注释。很多东西。而且还是不行……
有没有一种简单的方法可以做到这一点?当然我可以使用自定义的@Controller,但是我不能在我的其他实体中使用漂亮的rel。
我真的只需要 extends Repository<T, ID> 并创建一些自定义方法。还是我必须使用 CrudRepository 以便 Spring Data Rest 可以找到 findOne 和 findAll 方法?
编辑:
更准确地说:
我的应用程序有很多被 Spring Security 使用的硬编码 Permissions。每组权限都有自己的类。例如:
@Permission
class UserPermission {
public final static String RESET_PASSWORD = "USER_RESET_PASSWORD";
public final static String UPDATE_PROFILE = "USER_UPDATE_PROFILE";
}
现在还有一个名为PermissionGroup 的持久类,它被持久化到数据库。这基本上只是:
class PermissionGroup {
ID id;
List<String> permissions;
}
我想要的是,我从 Spring Data Rest 中获取那些典型的 URL,它们公开了我的权限。所以我可以使用这些 URL 引用向 PermissionGroup 添加/删除权限。即:
POST http://localhost:8080/app/permissionGroups
{
"permissions": [
{ "href" : "http://.../permissions/USER_RESET_PASSWORD" },
{ "href" : "http://.../permissions/USER_UPDATE_PROFILE" }
]
}
【问题讨论】:
-
嗨本杰明,您找到解决问题的方法了吗?
-
我还没有测试过,但看起来这(github.com/spring-projects/spring-data-keyvalue)可能是一个解决方案。
标签: spring spring-mvc spring-data spring-data-rest spring-data-commons