首页 > 解决方案 > 在 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 拥有静态的用户列表,该类提供了获取所有用户、从列表中删除用户等各种操作。

期待尽快得到解决方案,以便我可以在我的应用程序上取得进展。提前致谢。

标签: springspring-bootmicroservicesspring-hateoashateoas

解决方案


您可以发送 UserDaoService 的代码吗?我记得我有同样的问题,然后它得到了解决。还请查看下面的行,这看起来很可疑。

WebMvcLinkBuilder link = linkTo(this.getAllUsers());

推荐阅读