如何使用Spring-Hateoas以HAL格式获取响应

时间:2017-03-17 11:08:48

标签: java spring-boot spring-hateoas hypermedia

基本上我有与发布此question

的成员相同的问题

当我在我的应用程序中请求单个用户时,我得到了HAL格式的响应,就像我希望的那样

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,但它给了我相同的回复

1 个答案:

答案 0 :(得分:4)

您应该在Resources<UserResource>方法中返回list()

return new Resources(resources);

您还可以添加资源本身的自我链接以指向列表资源。

此外,我建议使用RessourceAssembler创建资源实例 - 请参阅http://docs.spring.io/spring-hateoas/docs/0.23.0.RELEASE/reference/html/#fundamentals.resource-assembler

此外,您可以向列表资源添加分页。 为此你需要:

  • 使用存储库中的findAll方法返回Page<User>
  • 您的控制器中的自动装配PagedResourcesAssembler<User>
  • 在您的列表方法中返回PagedResources<UserResource>
  • 使用PagedResourcesAssemblerPage转换为PagedResources

这会导致类似这样的事情:

    private final PagedResourcesAssembler<User> pagedResourcesAssembler;

    @RequestMapping(value = "users", method = RequestMethod.GET)
    public ResponseEntity<PagedResources<UserResource>> list(Pageable pageable) {
        final Page<User> page = repository.findAll(pageable);
        final Link link = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(this.getClass()).list(null)).withSelfRel();

        PagedResources<UserResource> resources = page.getContent().isEmpty() ?
                (PagedResources<UserResource>) pagedResourcesAssembler.toEmptyResource(page, ShippingZoneResource.class, link)
                : pagedResourcesAssembler.toResource(page, resourceAssembler, link);

        return ResponseEntity.ok(resources);
    }