【问题标题】:How to implement spring-hateoas on a spring boot app that use mybatis instead of jpa如何在使用 mybatis 而不是 jpa 的 spring boot 应用程序上实现 spring-hateoas
【发布时间】:2023-03-08 22:10:01
【问题描述】:

我正在尝试使用spring-hateoas 插件实现超媒体。

我没有像这个例子中那样的 JPA 休眠:https://spring.io/blog/2015/09/15/react-js-and-spring-data-rest-part-2-hypermedia

但我真的很喜欢这个结果。唯一的一点是我不想用JPA,我宁愿用mybatis。

我查看了 Greg Turniquist 的项目和文档,但我仍然不明白如何在我的项目中实现它。

我不想使用分页,但我没有任何 CrudRepository。

  1. 此项目仅适用于 JPA 吗?
  2. 有没有mybatis spring-boot spring-hatoas 实现的例子?
  3. 有人知道如何用mybatis实现吗?

【问题讨论】:

    标签: java spring spring-boot spring-hateoas


    【解决方案1】:

    hateoasJPA 的诀窍在于它开箱即用。不过,这并不意味着如果没有JPA,您就无法创建具有相同仇恨响应的 API。您只需创建自己的控制器并手动配置每个响应。

    有几种方法可以做到这一点。 Spring 有一个关于这个主题的很好的介绍性教程:https://spring.io/guides/gs/rest-hateoas/

    如果你想返回 hatoas 资源,你可以试试这样:

    @RequestMapping("/myEntity")
    public Resource<MyEntity> getMyEntity(String title) {
            MyEntity entity = // load your entity here
    
            // Provide a link to lookup of this resource
            Link entityLink = linkTo(MyEntityController).slash('/myEntity').withSelfRel()
            return new Resource<MyEntity>(entity, entityLink.expand(entity.entityId))
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 2019-03-31
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2020-08-31
      相关资源
      最近更新 更多