我似乎无法在我的build.gradle中正确实现任何HATEOAS元素:

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/

当我搜索该问题时,我找不到任何解决方案,并且我不太了解问题所在,因此我不知道可以尝试其他方法。

最佳答案

经过更多研究,我意识到spring.io教程已经过时了。经过一番尝试之后,IntelliJ开始显示org.springframework.hateoas,但是本教程提供的导入仍然无法正常工作。

我最终找到了到源代码的链接,并且代码已更新,而本教程没有。

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

基本上,Resource已替换为EntityModel,Resources已替换为CollectionModel,并且导入的结构已更改。

更新我的代码以使其与源代码匹配之后,在发送Employee的GET请求时,我得到了预期的响应:

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

07-28 01:56
查看更多