基本上我都喜欢谁张贴这question如何获得在HAL-格式与Spring-HATEOAS响应
,当我在我的应用程序请求单个用户的成员同样的问题,我得到的HAL响应-format,就像我希望
http://localhost:8080/api/v1/users/25与GET:
{
"userId": "25",
"firstname": "Beytullah",
"lastname": "Güneyli",
"username": "gueneylb",
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/users/25"
},
"roles": [
{
"href": "http://localhost:8080/api/v1/roles/33"
},
{
"href": "http://localhost:8080/api/v1/roles/34"
}
]
}
}
但是,当我要求所有的用户,我得到非HAL-格式的响应,这样的:
http://localhost:8080/api/v1/users与GET:
[...
{
"userId": "25",
"firstname": "Beytullah",
"lastname": "Güneyli",
"username": "gueneylb",
"links": [
{
"rel": "self",
"href": "http://localhost:8080/api/v1/users/25"
},
{
"rel": "roles",
"href": "http://localhost:8080/api/v1/roles/33"
},
{
"rel": "roles",
"href": "http://localhost:8080/api/v1/roles/34"
}
]
},
...]
这里是我的方法:
@RequestMapping(value = "users", method = RequestMethod.GET, produces = MediaTypes.HAL_JSON_VALUE)
public List<UserResource> list() throws MyException, NotFoundException {
List<User> userList= userRepository.findAll();
List<UserResource> resources = new ArrayList<UserResource>();
for (User user : userList) {
resources.add(getUserResource(user));
}
if(userList == null)
throw new MyException("List is empty");
else
return resources;
}
@RequestMapping(value = "users/{id}", method = RequestMethod.GET)
public UserResource get(@PathVariable Long id) throws NotFoundException {
User findOne = userRepository.findOne(id);
if (findOne == null){
log.error("Unexpected error, User with ID " + id + " not found");
throw new NotFoundException("User with ID " + id + " not found");
}
return getUserResource(findOne);
}
private UserResource getUserResource(User user) throws NotFoundException {
resource.add(linkTo(UserController.class).slash("users").slash(user.getId()).withSelfRel());
for(Role role : user.getRoles()){
resource.add(linkTo(RoleController.class).slash("roles").slash(role.getId()).withRel("roles"));
}
return resource;
}
你可以看到,这两种方法调用getUserResource(User user)
方法。
但是当我得到我的数据库中的所有用户时,_links
的格式不是我想要的。我认为这肯定是关于我回归的资源List
。也许是因为它没有HAL格式。我也尝试了Set
而不是List
,但它给了我相同的回应
谢谢你为你的非常详细和有益的答案!我发现了另一个解决这个问题的方法。我将'@EnableHypermediaSupport(type = {HypermediaType.HAL})'注释添加到我的控制器类中,并且它工作正常。但我喜欢你的建议,并会使用它。现在我将看看你建议的这种分页方式。非常感谢你 :) –