我可以使自定义控制器镜像Spring-Data-Rest/Spring-Hateoas生成的类的格式吗? [英] Can I make a custom controller mirror the formatting of Spring-Data-Rest / Spring-Hateoas generated classes?
问题描述
我正在尝试做一些我认为应该非常简单的事情.我有一个Question
对象,使用spring-boot,spring-data-rest和spring-hateoas进行设置.所有基础知识都可以正常工作.我想添加一个自定义控制器,该控制器以与Repository
的/questions
url GET完全相同的格式返回List<Question>
,以便两者之间的响应兼容.
I'm trying to do something I think should be really simple. I have a Question
object, setup with spring-boot, spring-data-rest and spring-hateoas. All the basics work fine. I would like to add a custom controller that returns a List<Question>
in exactly the same format that a GET to my Repository
's /questions
url does, so that the responses between the two are compatible.
这是我的控制者:
@Controller
public class QuestionListController {
@Autowired private QuestionRepository questionRepository;
@Autowired private PagedResourcesAssembler<Question> pagedResourcesAssembler;
@Autowired private QuestionResourceAssembler questionResourceAssembler;
@RequestMapping(
value = "/api/questions/filter", method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody PagedResources<QuestionResource> filter(
@RequestParam(value = "filter", required = false) String filter,
Pageable p) {
// Using queryDSL here to get a paged list of Questions
Page<Question> page =
questionRepository.findAll(
QuestionPredicate.findWithFilter(filter), p);
// Option 1 - default resource assembler
return pagedResourcesAssembler.toResource(page);
// Option 2 - custom resource assembler
return pagedResourcesAssembler.toResource(page, questionResourceAssembler);
}
}
选项1:依靠提供的SimplePagedResourceAssembler
Option 1: Rely on the provided SimplePagedResourceAssembler
此选项的问题是未呈现任何必需的_links
.如果有解决方案,那将是最简单的解决方案.
The problem with this option is none of the necessary _links
are rendered. If there was a fix for this, it would be the easiest solution.
选项2:实施我的开放资源汇编程序
该选项的问题是根据 Spring-Hateoas文档实现QuestionResourceAssembler
a>导致QuestionResource
最终成为Question
的近似副本的路径,然后汇编程序需要在两个对象之间手动复制数据,而我需要手工构建所有相关的_links
.这似乎浪费了很多精力.
The problem with this option is that implementing QuestionResourceAssembler
according to the Spring-Hateoas documentation leads down a path where the QuestionResource
ends up being a near-duplicate of Question
, and then the assembler needs to manually copy data between the two objects, and I need to build all the relevant _links
by hand. This seems like a lot of wasted effort.
该怎么办?
我知道Spring在导出QuestionRepository
时已经生成了执行所有这些操作的代码.我有什么办法可以使用该代码并使用它,以确保控制器的输出与所生成的响应是无缝的和可互换的?
I know Spring has already generated the code to do all this when it exports the QuestionRepository
. Is there any way I can tap into that code and use it, to ensure the output from my controller is seamless and interchangeable with the generated responses?
推荐答案
我找到了一种完全模仿Spring Data Rest行为的方法.诀窍在于使用PagedResourcesAssembler
和PersistentEntityResourceAssembler
的参数注入实例的组合.只需按如下所示定义您的控制器...
I've found a way to imitate the behavior of Spring Data Rest completely. The trick lies in using a combination of the PagedResourcesAssembler
and an argument-injected instance of PersistentEntityResourceAssembler
. Simply define your controller as follows...
@RepositoryRestController
@RequestMapping("...")
public class ThingController {
@Autowired
private PagedResourcesAssembler pagedResourcesAssembler;
@SuppressWarnings("unchecked") // optional - ignores warning on return statement below...
@RequestMapping(value = "...", method = RequestMethod.GET)
@ResponseBody
public PagedResources<PersistentEntityResource> customMethod(
...,
Pageable pageable,
// this gets automatically injected by Spring...
PersistentEntityResourceAssembler resourceAssembler) {
Page<MyEntity> page = ...;
...
return pagedResourcesAssembler.toResource(page, resourceAssembler);
}
}
这要感谢PersistentEntityResourceAssemblerArgumentResolver
的存在,Spring会使用它为您注入PersistentEntityResourceAssembler
.结果正是您从一种存储库查询方法所期望的结果!
This works thanks to the existence of PersistentEntityResourceAssemblerArgumentResolver
, which Spring uses to inject the PersistentEntityResourceAssembler
for you. The result is exactly what you'd expect from one of your repository query methods!
这篇关于我可以使自定义控制器镜像Spring-Data-Rest/Spring-Hateoas生成的类的格式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!