如何将自定义方法添加到 Spring Data Rest JPA 实现并利用 HATEOS 支持? [英] How to add custom methods to Spring Data Rest JPA implementation and leverage HATEOS support?

查看:37
本文介绍了如何将自定义方法添加到 Spring Data Rest JPA 实现并利用 HATEOS 支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 JPA 进行查询实现的 Spring Data Rest Repository 控制器,我需要添加一些使用 JPA 支持的标准 queryByExample 方法无法完成的自定义查询方法.我创建了一个具有必要方法的 Impl 类,但我无法识别它.我看到我可以使用标准的 Spring MVC 控制器,但我想要一个统一的 API,基本上我真正想要的是实现我自己的自定义/search 方法.

I have a Spring Data Rest Repository controller that utilizes JPA for the query implementation, and I need to add some custom query methods that cannot be done using the standard queryByExample method that JPA supports. I have created an Impl class that has the necessary method, but I cannot get it to be recognized. I saw that I can utilize a standard Spring MVC Controller, but I want to have a unified API, and basically all I really want is to implement my own custom /search methods.

即使使用自定义控制器,问题是不再提供 HAL 链接和其他相关项目.

Even with the custom controller, the problem is then that the HAL links and other related items are no longer provided.

Spring 人员能否花一些时间让某人记录如何做一些更高级的事情?我猜有时必须实现自己的搜索方法是相当普遍的,而且花时间弄清楚如何做到这一点是值得的.

Can the Spring folks spend some time having someone document how to do some of this more advanced stuff? I'm guessing that having to implement your own search methods at times are fairly common, and it would be time well spent to make it clear how to do this.

推荐答案

一个简单的实现可能如下所示:

A simple implementation could look like this:

@BasePathAwareController
class CustomInvocationsController implements ResourceProcessor<RepositorySearchesResource> {

  private final YourRepository repository;

  public CustomInvocationsController(YourRepository repository) {
    this.repository = repository;
  }

  @RequestMapping(…)
  ResponseEntity<?> handleRequest(PersistentEntityResourceAssembler assembler)

    // invoke repository
    // Use assembler to build a representation
    // return ResponseEntity
  }

  @Override
  public RepositorySearchesResource process(RepositorySearchesResource resource) {
    // add Link to point to the custom handler method
  }
}

注意事项:

  • 使用 @BasePathAwareController 而不是普通的 @Controller 确保无论您将处理程序方法映射到什么,它都会考虑您在 Spring 上配置的基本路径数据 REST 也是如此.
  • 在请求映射中,使用您从 Spring MVC 中已知的所有内容,选择合适的 HTTP 方法.
  • PersistentEntityResourceAssembler 基本上抽象了在 PersistentEntityResource 中设置表示模型,以便 Spring Data REST 对关联等的特定处理开始(关联变成链接等).立>
  • 实现 ResourceProcessor 以对渲染所有搜索的资源返回的 RepositorySearchesResource 进行后处理.目前,无法确定该资源呈现的是哪种域类型.我提交并修复了 DATAREST-515 以改进这一点.
  • using @BasePathAwareController instead of a plain @Controller makes sure whatever you're mapping the handler method to, it will consider the base path you've configured on Spring Data REST, too.
  • within the request mapping, use everything you already know from Spring MVC, choose an appropriate HTTP method.
  • PersistentEntityResourceAssembler basically abstracts setting up a representation model within a PersistentEntityResource so that the Spring Data REST specific treatment of associations etc. kicks in (associations becoming links etc.
  • implement ResourceProcessor to post-process RepositorySearchesResource which is returned for the resource rendering all searches. Currently, there's no way to determine which domain type that resource was rendered. I filed and fixed DATAREST-515 to improve that.

这篇关于如何将自定义方法添加到 Spring Data Rest JPA 实现并利用 HATEOS 支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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