findBy URI 在 Spring Data Rest 中不起作用 [英] findBy URI not working in Spring Data Rest

查看:47
本文介绍了findBy URI 在 Spring Data Rest 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,在 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)

http://localhost:8080/repositories/students/search/findByTeacher?teacher=http://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".

我错过了什么吗?

推荐答案

  1. 你可以通过配置 web 初始化器来暴露 @Id

  1. 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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆