findBy URI 在 Spring Data Rest 中不起作用

编程入门 行业动态 更新时间:2024-10-27 14:31:38
本文介绍了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) 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 中不起作用

    本文发布于:2023-11-22 17:24:18,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1618343.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中不   URI   findBy   Spring   Rest

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!