1

默认情况下,在 Spring Data Rest 中,实体的 @Id 是不公开的。根据 REST 规则,我们应该使用资源的 URI 来引用它。鉴于此假设,如果您将 URI 传递给 findBy 查询,它们应该可以工作,但它们不会。

例如,假设我在老师和学生之间有一对多的关系。我想按老师找学生。

List<Student> findByTeacher(Teacher teacher)

http://localhost:8080/repositories/students/search/findByTeacher?teacher=http://localhost:8080/repositories/teachers/1

这不起作用,因为框架正在尝试将教师 URI 转换为 Long。我收到此错误消息“无法从 java.lang.String 类型转换为 java.lang.Long 类型”。

我错过了什么吗?

4

2 回答 2

2
  1. 您可以通过配置 web 初始化程序公开 @Id

    //Web 初始化器 @Configuration public static class RespositoryConfig extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration( RepositoryRestConfiguration config) { config.exposeIdsFor(Teacher.class); } }

  2. 将列表更改为页面很好

    列出 findByTeacher(教师老师)

Page<Student> findByTeacher(@Param("teacher) Teacher teacher, Pageable pageable);

另请注意 @Param 注释与 Pageable 一起需要。后者是必需的,因为返回类型“页面”

3.最新快照,而不是里程碑工作正常

于 2014-01-22T20:07:18.420 回答
1

https://jira.spring.io/browse/DATAREST-502

根据您的 Spring Data 版本,它可以按您的意愿工作。如果您使用 Spring Data 2.4,则需要传递 URI。如果您使用的是以前的版本,则需要传递 id。

于 2015-08-13T08:21:33.217 回答