spring 何时使用 @RestController 与 @RepositoryRestResource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22824840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:39:18  来源:igfitidea点击:

When to use @RestController vs @RepositoryRestResource

springspring-mvcspring-dataspring-data-restspring-hateoas

提问by code

I have been looking at various examples of how to use Spring with REST. Our end target is a Spring HATEOAS/HALsetup

我一直在研究如何将 Spring 与REST结合使用的各种示例。我们的最终目标是 SpringHATEOAS/HAL设置

I have seen two distinct methods for rendering RESTwithin Spring

我见过两种不同的在Spring 中渲染REST 的方法

  1. Via @RestControllerwithin a Controller

  2. Via @RepositoryRestResourcewithin a Repository

  1. 通过@RestController控制器内

  2. 通过@RepositoryRestResource存储库中

The thing I am struggling to find is why would you use one over the other. When trying to implement HALwhich is best?

我正在努力寻找的事情是你为什么要使用一个而不是另一个。在尝试实施HAL哪个最好时?

Our database backend is Neo4j.

我们的数据库后端是Neo4j

采纳答案by zpontikas

Ok, so the short story is that you want to use the @RepositoryRestResourcesince this creates a HATEOASservice with Spring JPA.

好的,所以简短的故事是您想要使用 ,@RepositoryRestResource因为这会使用Spring JPA创建一个HATEOAS服务。

As you can see hereadding this annotation and linking it to your Pojo you have a fully functional HATEOASservice without having to implement the repository method or the REST service methods

正如您在此处看到的添加此注释并将其链接到您的 Pojo,您就拥有了一个功能齐全的HATEOAS服务,而无需实现存储库方法或 REST 服务方法

If you add the @RestControllerthen you have to implement each method that you want to expose on your own and also it does not export this to a HATEOASformat.

如果您添加,@RestController那么您必须实现您想要自己公开的每个方法,并且它不会将其导出为HATEOAS格式。

回答by Jacob Creed

There is a third (and fourth) option that you have not outlined, which is to use either @BasePathAwareController or @RepositoryRestController, depending on whether you are performing entity-specific actions or not.

您还没有概述第三个(和第四个)选项,即使用 @BasePathAwareController 或 @RepositoryRestController,具体取决于您是否正在执行特定于实体的操作。

@RepositoryRestResource is used to set options on the public Repository interface - it will automatically create endpoints as appropriate based on the type of Repository that is being extended (i.e. CrudRepository/PagingAndSortingRepository/etc).

@RepositoryRestResource 用于在公共 Repository 接口上设置选项 - 它会根据正在扩展的 Repository 类型(即 CrudRepository/PagingAndSortingRepository/等)自动创建适当的端点。

@BasePathAwareController and @RepositoryRestController are used when you want to manually create endpoints, but want to use the Spring Data REST configurations that you have set up.

@BasePathAwareController 和 @RepositoryRestController 当您想要手动创建端点,但想要使用您已设置的 Spring Data REST 配置时使用。

If you use @RestController, you will create a parallel set of endpoints with differentconfiguration options - i.e. a different message converter, different error handlers, etc - but they will happily coexist (and probably cause confusion).

如果您使用@RestController,您将创建一组具有不同配置选项的并行端点- 即不同的消息转换器、不同的错误处理程序等 - 但它们会很高兴地共存(并且可能会引起混淆)。

Specific documentation can be found here.

可以在此处找到特定文档。

回答by shaILU

Well, above answers are correct in their context still I am giving you practical example.

好吧,上面的答案在他们的上下文中是正确的,但我仍然在给你实际的例子。

In many scenarios as a part of API we need to provide endpoints for searching an entity based on certain criteria. Now using JPA you don't have to even write queries, just make an interface and methods with specific nomenclature of Spring-JPA. To expose such APIs you will make Service layer which would simply call these repository methods and finally Controllers which will expose endpoints by calling Service layer.

在许多场景中,作为 API 的一部分,我们需要提供用于基于特定条件搜索实体的端点。现在使用 JPA,您甚至不必编写查询,只需使用 Spring-JPA 的特定命名法创建接口和方法即可。要公开此类 API,您将创建将简单调用这些存储库方法的服务层和最终通过调用服务层公开端点的控制器。

What Spring did here, allow you to expose these endpoints from such interfaces (repositories) which are generally GET calls to search entity and in background generates necessary files to create final endpoints. So if you are using @RepositoryRestResource then there is no need to make Service/Controller layer.

Spring 在这里所做的,允许您从这些接口(存储库)公开这些端点,这些接口通常是对搜索实体的 GET 调用,并在后台生成必要的文件来创建最终端点。因此,如果您使用的是 @RepositoryRestResource,则无需创建 Service/Controller 层。

On the other hand @RestController is a controller that specifically deals with json data and rest work as a controller. In short @Controller + @ResponseBody = @RestController.

另一方面,@RestController 是一个控制器,专门处理 json 数据和作为控制器的休息工作。简而言之@Controller + @ResponseBody = @RestController。

Hope this helps.

希望这可以帮助。

See my working example and blog for the same:
http://sv-technical.blogspot.com/2015/11/spring-boot-and-repositoryrestresource.html
https://github.com/svermaji/Spring-boot-with-hibernate-no-controller

请参阅我的工作示例和博客:
http: //sv-technical.blogspot.com/2015/11/spring-boot-and-repositoryrestresource.html
https://github.com/svermaji/Spring-boot-with -hibernate-no-controller

回答by Grigory Kislin

@RepositoryRestControlleroverride default generated Spring Data REST controllers from exposed repository.

@RepositoryRestController从公开的存储库覆盖默认生成的 Spring Data REST 控制器。

To take advantage of Spring Data REST's settings, message converters, exception handling, and more, use the @RepositoryRestControllerannotation instead of a standard Spring MVC @Controlleror @RestController

要利用 Spring Data REST 的设置、消息转换器、异常处理等,请使用@RepositoryRestController注释而不是标准的 Spring MVC@Controller@RestController

E.g this controllers use spring.data.rest.basePathSpring Boot setting as base path for routing.

例如,这个控制器使用spring.data.rest.basePathSpring Boot 设置作为路由的基本路径。

See Overriding Spring Data REST Response Handlers.

请参阅覆盖 Spring 数据 REST 响应处理程序

Be aware of adding @ResponseBodyas it is missed in @RepositoryRestController

请注意添加,@ResponseBody因为它错过了@RepositoryRestController

If you not exposed repository (marked as @RepositoryRestResource(exported = false)), use @BasePathAwareControllerannotation instead

如果您未公开存储库(标记为@RepositoryRestResource(exported = false)),请改用@BasePathAwareController注释

Also be aware of bags

还要注意袋子

ControllerLinkBuilderdoes not take Spring Data REST's base path into account and @RequestMappingshouldn't be used on class/type level

ControllerLinkBuilder不考虑 Spring Data REST 的基本路径,@RequestMapping不应在类/类型级别使用

and

Base path doesn't show up in HAL

基本路径未显示在 HAL 中

Workaround to fix link: https://stackoverflow.com/a/51736503/548473

修复链接的解决方法:https: //stackoverflow.com/a/51736503/548473

UPDATE: at last I prefer not to use @RepositoryRestControllerdue to lot of workarounds.

更新:@RepositoryRestController由于有很多变通方法,我终于不想使用了。