默认情况下,在 Spring Data Rest 中,实体的 @Id 不会公开.根据 REST 规则,我们应该使用资源的 URI 来引用它.鉴于此假设,如果您将 URI 传递给 findBy 查询,它们应该会起作用,但它们不会.
By default, in Spring Data Rest the @Id of the entity is not exposed. In line with the REST rules, we're supposed to use the URI of the resource to refer to it. Given this assumption, the findBy queries should work if you pass a URI to them, but they don't.
例如,假设我在教师和学生之间存在一对多关系.我想按老师找学生.
For example, say I have a one-to-many relationship between Teacher and Student. I want to find students by teacher.
List<Student> findByTeacher(Teacher teacher) localhost:8080/repositories/students/search/findByTeacher?teacher=localhost:8080/repositories/teachers/1这不起作用,因为框架正在尝试将教师 URI 转换为 Long.我收到此错误消息,提示无法从 java.lang.String 类型转换为 java.lang.Long 类型".
This doesn't work because the framework is attempting to convert the teacher URI to a Long. I get this error that says "Failed to convert from type java.lang.String to type java.lang.Long".
我错过了什么吗?
推荐答案你可以通过配置 web 初始化器来暴露 @Id
You could expose @Id s by configuring web intializer
//网页初始化器@配置公共静态类 RespositoryConfig 扩展RepositoryRestMvcConfiguration {@覆盖protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration 配置){config.exposeIdsFor(Teacher.class);}}
//Web intializer @Configuration public static class RespositoryConfig extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration( RepositoryRestConfiguration config) { config.exposeIdsFor(Teacher.class); } }
把List改成Page就好了
Its good to change List to Page
列出 findByTeacher(Teacher 老师)
List findByTeacher(Teacher teacher)
到
Page<Student> findByTeacher(@Param("teacher) Teacher teacher, Pageable pageable);还要注意@Param 注释与Pageable 一起是必需的.后者是必需的,因为返回类型页面"
Also note @Param annotation is required along with Pageable. The latter is required because return type "Page"
3.最新快照,而不是里程碑工作正常
3.Latest snapshots, not milestones work fine
更多推荐
findBy URI 在 Spring Data Rest 中不起作用
发布评论