Mar*_*tti 12 spring-data-jpa spring-data-rest spring-hateoas
我有一个Spring Data Rest Repository控制器,它使用JPA进行查询实现,我需要添加一些使用JPA支持的标准queryByExample方法无法完成的自定义查询方法.我创建了一个具有必要方法的Impl类,但我不能让它被识别.我看到我可以使用标准的Spring MVC Controller,但我希望有一个统一的API,基本上我真正想要的是实现我自己的自定义/搜索方法.
即使使用自定义控制器,问题仍然是不再提供HAL链接和其他相关项目.
春天的人们可以花一些时间让某人记录如何做一些这些更高级的东西吗?我猜测,有时必须实现自己的搜索方法是相当普遍的,并且花费时间来明确如何做到这一点.
Oli*_*ohm 10
一个简单的实现可能如下所示:
@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
}
}
Run Code Online (Sandbox Code Playgroud)
有几点需要注意:
@BasePathAwareController而不是普通使用@Controller确保将处理程序方法映射到的任何内容,它将考虑您在Spring Data REST上配置的基本路径.PersistentEntityResourceAssembler基本上抽象在一个表达模型中设置一个表示模型,PersistentEntityResource以便Spring Data REST特定的关联等处理开始(协会成为链接等)ResourceProcessor后处理RepositorySearchesResource,为资源呈现所有搜索返回.目前,无法确定该资源所呈现的域类型.我提交并修复了DATAREST-515以改进它.| 归档时间: |
|
| 查看次数: |
5927 次 |
| 最近记录: |