spring - 在 Spring Boot 应用程序中实现 HATEOAS 时出错:java.lang.IllegalArgumentException
问题描述
我正在开发 spring-boot 应用程序,我创建了一个用于用户管理的小型应用程序,并进行了 get-user、create-user 等操作。这些操作在我使用 HATEOAS 之前运行良好。现在,当我从 POSTMAN 访问我的网络服务时,我遇到了错误。
邮递员中的错误:
{
"timestamp": "2021-05-17T16:01:22.049+00:00",
"message": **"Object of class [java.util.ArrayList] must be an instance of interface org.springframework.hateoas.server.core.LastInvocationAware"**,
"details": "uri=/user/get-user/100",
"className": "java.lang.IllegalArgumentException"
}
我用来访问网络服务的路径:http://localhost:8080/user/get-user/100
我的静态列表中确实有一个 id 为 100 的用户,并且在编译中没有错误。
这是我的 @RestController 注释类:
all required imports here >
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping(path = "/user/get-users")
public List<User> getAllUsers() {
return service.getUserList();
}
@RequestMapping(method = RequestMethod.GET, path = "/user/get-user/{id}")
public EntityModel<User> getUserByIDWithURL(@PathVariable int id) {
System.out.println("Request for ID: " + id);
User user = service.findUser(id);
if (user == null) {
throw new UserNotFoundException("User not found with ID " + id);
}
EntityModel<User> resource = EntityModel.of(user);
WebMvcLinkBuilder link = linkTo(this.getAllUsers());
resource.add(link.withRel("all-users"));
return resource;
}
}
注意:UserDaoService 拥有静态的用户列表,该类提供了获取所有用户、从列表中删除用户等各种操作。
期待尽快得到解决方案,以便我可以在我的应用程序上取得进展。提前致谢。
解决方案
您可以发送 UserDaoService 的代码吗?我记得我有同样的问题,然后它得到了解决。还请查看下面的行,这看起来很可疑。
WebMvcLinkBuilder link = linkTo(this.getAllUsers());
推荐阅读
- java - 获取elasticsearch中搜索值匹配的字段名称
- drop-down-menu - 手风琴内的 Bootstrap 4 下拉菜单
- solr - Solr如何在搜索结果中区分大小写字符
- css - 隐式网格有什么我不能用显式网格做的吗?
- reactjs - 发送函数 throw `spread operator` 不起作用,但添加了状态值
- sql - 以函数为值执行 \set 命令
- c++ - How to produce compiler error if default template is used?
- python - Python 3.7 上的触发器(Networktest)安装
- javascript - 如何在javascript中将blob下载为mp4?
- mysql - Rewrite correlated subquery in SELECT into JOIN statement