Jef*_*ier 0 java gradle hateoas spring-boot
我无法导入任何 HATEOAS 元素,尽管它似乎在我的 build.gradle 中正确实现:
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
Run Code Online (Sandbox Code Playgroud)
这是我的进口:
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.*;
Run Code Online (Sandbox Code Playgroud)
以下是错误:
$ ./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
Run Code Online (Sandbox Code Playgroud)
我认为它不相关,但我正在使用 IntelliJ 并尝试完成本教程:https : //spring.io/guides/tutorials/bookmarks/
我在 google 上搜索问题时找不到任何解决方案,我也不太明白问题是什么,所以我不知道还能尝试什么。
经过更多的研究,我意识到 spring.io 教程已经过时了。玩了一会儿之后,IntelliJ 开始显示 org.springframework.hateoas,但是教程提供的导入仍然不起作用。
最终找到了源代码的链接,代码已经更新,教程没有。
https://github.com/spring-guides/tut-rest/tree/master/rest/src/main/java/payroll
基本上,Resource 已被 EntityModel 替换,Resources 已被 CollectionModel 替换,并且导入的结构已更改。
更新我的代码以匹配源代码后,当我为员工发送 GET 请求时,我得到了预期的响应:
{"id":1,"name":"Bilbo Baggins","role":"burglar","_links":{"self":{"href":"http://localhost:8080/employees/1"},"employees":{"href":"http://localhost:8080/employees"}}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1476 次 |
| 最近记录: |