gpt4 book ai didi

java - 如何使用 Spring-Hateoas 获得 HAL 格式的响应

转载 作者:搜寻专家 更新时间:2023-11-01 02:37:45 26 4
gpt4 key购买 nike

基本上我和发布这个 question 的成员(member)有同样的问题

当我在我的应用程序中请求单个用户时,我得到了 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 但它给了我相同的响应

最佳答案

你应该返回 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> 的方法.
  • Autowiring PagedResourcesAssembler<User>在你的 Controller 中
  • 返回PagedResources<UserResource>在你的列表方法中
  • 使用PagedResourcesAssembler转换 Page进入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);
}

关于java - 如何使用 Spring-Hateoas 获得 HAL 格式的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com