【问题标题】:Adding _links ,_refs to Spring application using hateos使用 hatoas 添加链接,引用到 Spring 应用程序
【发布时间】:2023-04-03 16:10:01
【问题描述】:

我有一篇 Spring Boot 服务文章。我正在使用弹簧数据休息和 mongoDB。我正在覆盖自定义控制器中的 findAll() 方法。但在回复中,我缺少 _links 和页面详细信息。当自定义控制器中的 findAll() 方法被覆盖时,我正在寻找一种方法来获取 _links 和页面信息

预期响应:

"_links": {
        "self": {
            "href": "http://localhost:8080/article"
        },
        "profile": {
            "href": "http://localhost:8080/profile/articles"
        },
        "search": {
            "href": "http://localhost:8080/articles/search"
        },
        "page": {
            "size": 20,
            "totalElements": 61,
            "totalPages": 5,
            "number": 0
        }
    }
}

【问题讨论】:

  • 您需要添加 spring-boot-starter-hateoas 依赖并相应地执行更改https://www.baeldung.com/spring-hateoas-tutorial。请检查
  • 向我们展示您的相关代码,以便我们更好地理解以帮助您
  • 您需要确保返回适当的表示。 (指定您的 Spring HATEOAS 的确切版本,因为这在 v1.0 中发生了巨大变化。)

标签: java spring spring-boot spring-data-mongodb spring-data-rest


【解决方案1】:

在 pom.xml 中添加以下依赖项或在 build.gradle 中添加相关的 gradle 依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

并使用 extends ResourceSupport

扩展模型对象

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 2019-10-22
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多