何时使用 @RestController 与 @RepositoryRestResource [英] When to use @RestController vs @RepositoryRestResource
问题描述
我一直在研究如何将 Spring 与 REST 结合使用的各种示例.我们的最终目标是 Spring HATEOAS/HAL
设置
I have been looking at various examples of how to use Spring with REST. Our end target is a Spring HATEOAS/HAL
setup
我在 Spring
I have seen two distinct methods for rendering REST within Spring
通过控制器内的
@RestController
通过存储库中的@RepositoryRestResource
我正在努力寻找的是,你为什么要使用一个而不是另一个.尝试实现 HAL
时哪个最好?
The thing I am struggling to find is why would you use one over the other. When trying to implement HAL
which is best?
我们的数据库后端是 Neo4j.
Our database backend is Neo4j.
推荐答案
好吧,简单来说就是你想使用 @RepositoryRestResource
因为这会创建一个 HATEOAS Spring JPA 的服务.
Ok, so the short story is that you want to use the @RepositoryRestResource
since this creates a HATEOAS service with Spring JPA.
如您所见此处添加此注释并链接它对于您的 Pojo,您拥有一个功能齐全的 HATEOAS 服务,而无需实现存储库方法或 REST 服务方法
As you can see here adding this annotation and linking it to your Pojo you have a fully functional HATEOAS service without having to implement the repository method or the REST service methods
如果您添加 @RestController
,那么您必须自己实现每个要公开的方法,并且它不会将其导出为 HATEOAS 格式.
If you add the @RestController
then you have to implement each method that you want to expose on your own and also it does not export this to a HATEOAS format.
这篇关于何时使用 @RestController 与 @RepositoryRestResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!