【问题标题】:Cannot import HATEOAS elements in Spring Boot Gradle project无法在 Spring Boot Gradle 项目中导入 HATEOAS 元素
【发布时间】:2020-06-19 09:58:18
【问题描述】:

我无法导入任何 HATEOAS 元素,尽管它似乎已在我的 build.gradle 中正确实现:

implementation 'org.springframework.boot:spring-boot-starter-hateoas'

这是我的导入:

import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

以下是错误:

$ ./gradlew build

> Task :compileJava FAILED
path\src\main\java\payroll\EmployeeController.java:5: error: cannot find symbol
import org.springframework.hateoas.Resource;
                                  ^
  symbol:   class Resource
  location: package org.springframework.hateoas
path\src\main\java\payroll\EmployeeController.java:6: error: cannot find symbol
import org.springframework.hateoas.Resources;
                                  ^
  symbol:   class Resources
  location: package org.springframework.hateoas
path\src\main\java\payroll\EmployeeController.java:15: error: package org.springframework.hateoas.mvc does not exist
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
                                             ^
path\src\main\java\payroll\EmployeeController.java:41: error: cannot find symbol
  Resource<Employee> one(@PathVariable Long id) {
  ^
  symbol:   class Resource
  location: class EmployeeController
4 errors

我认为这无关紧要,但我正在使用 IntelliJ 并尝试完成本教程:https://spring.io/guides/tutorials/bookmarks/

我在谷歌上搜索该问题时找不到任何解决方案,而且我真的不明白问题是什么,所以我不知道还能尝试什么。

【问题讨论】:

    标签: java spring-boot gradle hateoas


    【解决方案1】:

    您可能发现了一个过时的教程。现在您需要将应用程序代码迁移到较新版本的 HATEOAS。

    • 参见Migrating to Spring HATEOAS 1.0 指南
    • 你可以在GitHub上找到官方migration script。只需下载它,从您的项目根目录运行它。默认情况下,它将检查所有 Java 源文件并将旧的 Spring HATEOAS 类型引用替换为新的。

    【讨论】:

      【解决方案2】:

      经过更多研究,我意识到 spring.io 教程已经过时了。玩了一会儿,IntelliJ 开始显示 org.springframework.hateoas,但是教程提供的导入仍然不起作用。

      我终于找到了源代码的链接,代码已经更新了,而教程还没有。

      https://github.com/spring-guides/tut-rest/tree/master/rest/src/main/java/payroll

      基本上,Resource 已经被 EntityModel 取代,Resources 已经被 CollectionModel 取代,import 的结构也发生了变化。

      更新我的代码以匹配源代码后,当我发送一个员工的 GET 请求时,我得到了预期的响应:

      {"id":1,"name":"Bilbo Baggins","role":"burglar","_links":{"self":{"href":"http://localhost:8080/employees/1"},"employees":{"href":"http://localhost:8080/employees"}}}
      

      【讨论】:

      • 不错。我想知道谷歌搜索“找不到符号 EntityModel”是否会找到我的 HATEOS 错误的答案
      猜你喜欢
      • 2016-07-20
      • 2019-01-27
      • 2020-04-06
      • 2020-10-26
      • 2020-09-25
      • 2018-10-27
      • 2019-05-17
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多