我正在尝试使用spring boot实现Hateoas。在我的UserController
课程中,我使用了以下代码
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
User user = service.findOne(id);
Resource<User> resource = new Resource<User>(user);
ControllerLinkBuilder linkTo=linkTo(methodOn(this.getClass()).retrieveAllUsers());
我正在使用 - methodOn()
获取编译时错误。编译器无法使用控制器链接构建器找到methodOn。
我使用下面的导入来实现hateoas
import org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
您在此处使用的方法是静态的,因此要使用它们,您需要使用类引用进行访问,除非使用static import
s导入它们。查看更多详情here