何时使用@RestController与@RepositoryRestResource [英] When to use @RestController vs @RepositoryRestResource
问题描述
我一直在研究有关如何在 REST 中使用Spring的各种示例.我们的最终目标是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 .
推荐答案
好,因此,您要使用@RepositoryRestResource
是因为这会使用创建 HATEOAS 服务春季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屋!