在自定义@RepositoryRestController方法中填充实体链接 [英] Filling entity links in custom @RepositoryRestController methods

查看:1536
本文介绍了在自定义@RepositoryRestController方法中填充实体链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring-data-rest来为一些JPA实体提供读取API。
对于写入,我需要发出Command对象而不是直接写入DB,所以我使用 @RepositoryRestController 和各种命令处理方法添加了一个自定义控制器:

I am using Spring-data-rest to provide read APIs over some JPA entities. For writes I need to issue Command objects rather than directly write to the DB, so I added a custom controller using @RepositoryRestController and various command handling methods:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody MyEntity post(@RequestBody MyEntity entity) {
  String createdId = commands.sendAndWait(new MyCreateCommand(entity));
  return repo.findOne(createdId);
}

我希望输出能够像春天的任何其他响应一样丰富-data-rest控制器,特别是我想让它添加HATEOAS链接到它自己及其关系。

I would like the output to be enriched just like any other response by the spring-data-rest controller, in particular I want it to add HATEOAS links to itself and its relations.

推荐答案

这是最近的通过回答(参见第3点) -gierke> Oliver Gierke 本人(问题使用了截然不同的关键词,所以我不会将其标记为重复)。

This has recently been answered (see point 3.) by Oliver Gierke himself (the question used quite different keywords though, so I won't flag this as duplicate).

一个例子单个实体将成为:

The example for a single entity would become:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody PersistentEntityResource post(@RequestBody MyEntity entity,
    PersistentEntityResourceAssembler resourceAssembler)) {
  String createdId = commands.sendAndWait(new MyCreateCommand(entity));
  return resourceAssembler.toResource(repo.findOne(createdId));
}

非分页列表的示例:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Resources<PersistentEntityResource> post(
    @RequestBody MyEntity entity,
    PersistentEntityResourceAssembler resourceAssembler)) {
  List<MyEntity> myEntities = ...
  List<> resources = myEntities
      .stream()
      .map(resourceAssembler::toResource)
      .collect(Collectors.toList());
  return new Resources<PersistentEntityResource>(resources);
}

最后,对于分页响应,应该使用注入的PagedResourcesAssembler,传入方法注入的ResourceAssembler和Page,而不是实例化Resources。有关如何使用 PersistentEntityResourceAssembler PagedResourcesAssembler 的更多详细信息,请参阅这个答案。请注意,目前这需要使用原始类型和未经检查的强制转换。

Finally, for a paged response one should use an injected PagedResourcesAssembler, passing in the method-injected ResourceAssembler and the Page, rather than instantiating Resources. More details about how to use PersistentEntityResourceAssembler and PagedResourcesAssembler can be found in this answer. Note that at the moment this requires to use raw types and unchecked casts.

可能有自动化空间,欢迎使用更好的解决方案。

There is room for automation maybe, better solutions are welcome.

PS:我还创建了一个 JIRA票来添加这个到Spring Data的文档。

P.S.: I also created a JIRA ticket to add this to Spring Data's documentation.

这篇关于在自定义@RepositoryRestController方法中填充实体链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆