Java Spring HATEOAS 与 Spring Data Rest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19514131/
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-08-12 17:55:15  来源:igfitidea点击:

Spring HATEOAS versus Spring Data Rest

javaspringspring-data-resthateoasspring-hateoas

提问by HighTML

Question is, what's the difference between Spring HATEOAS versus Spring Data Rest ?

问题是,Spring HATEOAS 与 Spring Data Rest 之间有什么区别?

I feel both can do the same, and Spring Data Rest (as part of Spring Data) seems a bit more alive.

我觉得两者都可以做同样的事情,而且 Spring Data Rest(作为 Spring Data 的一部分)似乎更有活力。

https://github.com/spring-projects/spring-hateoashttps://github.com/spring-projects/spring-data-rest

https://github.com/spring-projects/spring-hateoas https://github.com/spring-projects/spring-data-rest

When would you use one or the other?

你什么时候会使用其中一个?

采纳答案by Oliver Drotbohm

Spring HATEOAS provides common abstractions (representational models, a Linkclass, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.

Spring HATEOAS 提供了通用抽象(表示模型、Link类、用于构建指向 Spring MVC 控制器的链接的 API 等),以简化使用 Spring MVC 构建超媒体驱动的 REST API。因此,您可以将它与 Spring MVC 一起使用来手动构建这些服务。

Spring Data REST uses Spring HATEOAS to automatically expose resources for entities managed by Spring Data repositories and leverages hypermedia aspects to do pagination, link entities etc. So it covers the 80% use case for the basic stuff and allows you to selectively add more complex processes using manually implemented controllers later on.

Spring Data REST 使用 Spring HATEOAS 为 Spring Data 存储库管理的实体自动公开资源,并利用超媒体方面进行分页、链接实体等。因此它涵盖了基本内容的 80% 用例,并允许您有选择地添加更复杂的流程稍后使用手动实现的控制器。

To get a feel for this, feel free to have a look at the Spring RESTBuckssample project. The handling of Orderinstances is completely done by Spring Data REST (with some minor tweaks to implement business constraints). The entire payment logic is then implemented manually as the process does not fall into the CRUD category as we actually need to implement certain steps and a protocol to complete the order. Again, the code is here, a slide deck with some additional visuals can be found at speakerdeck.com.

要对此有所了解,请随时查看Spring RESTBucks示例项目。Order实例的处理完全由 Spring Data REST 完成(通过一些小的调整来实现业务约束)。然后整个支付逻辑是手动实施的,因为该过程不属于 CRUD 类别,因为我们实际上需要实施某些步骤和协议来完成订单。再次,代码在这里,可以在speakerdeck.com上找到带有一些附加视觉效果的幻灯片。

回答by Hezi Schrager

I chose to use both of them in my project. One layer of controllers was built with Spring Data REST. The other layer of controllers was @RestController's (spring-wevmvc). In this layer I used Spring HATEOAS to create customed page's. (Process was : 1.creating Pageable Pageable pageable = new PageRequest2. creating new Page Page<FooDt> page = new PageImpl<FooDt>3.creating PagedResources PagedResources<Resource<FooDt>> resource = fooAssembler.toResource(page, fooAssembler)after that process using Hymanson's ObjectMapper to return json.

我选择在我的项目中同时使用它们。一层控制器是用 Spring Data REST 构建的。另一层控制器是@RestController's (spring-wevmvc)。在这一层中,我使用 Spring HATEOAS 创建自定义页面。(过程是:1 Pageable pageable = new PageRequest.创建Pageable 2. 创建新页面Page<FooDt> page = new PageImpl<FooDt>3.PagedResources<Resource<FooDt>> resource = fooAssembler.toResource(page, fooAssembler)在使用 Hymanson 的 ObjectMapper 返回 json 的过程之后创建 PagedResources 。

The solution that I found for loading to context both technologies - is using two DispatcherServlet's. Otherwise, Spring Data Rest is taking control and there is no option to use other controllers. ( In that way I had two domains in my app. One for Data Rest and one for webmvc+HATEOS).

我找到的将这两种技术加载到上下文的解决方案是使用两个 DispatcherServlet。否则,Spring Data Rest 正在控制并且没有使用其他控制器的选项。(这样我的应用程序中有两个域。一个用于 Data Rest,一个用于 webmvc+HATEOS)。

回答by Valerio Vaudi

HATEOAS stand for Hypermedia as the Engine of Application State and is one of the key ponit of REST. Basically the key point consist to use links on the your resource representation for map the valid transition of the application state. In this case will be the service provider that provide the valid next correct state of the your application reachable through the link. Spring HATEOAS is the Spring projects for help to build the Hymeridia Controls in your Resource. It is a project integrate with Spring MVC and you can think as the Spring MVC extension for building a real RESTFull WS whit a very good support for increase the level of the your service form CRUD (level 2 of maturity in the Richardson model) to an Hypermedia aware (level 3 of maturity in the Richardson model). Spring Data Rest on the other hands is a very nice project that use Spring HATEOAS as basic brick, for give you a repository layer usable as restfull ws. In proctis the project help to reduce the classical boliporlent code for expose the your repository layer as a restfull endpoint. We can say tat was the propouse of the projects very different. With Spring HATEOAS you had a framework usable for any kind of restfull endpoint, with spring data rest you had a spring project that already provide an endpoint and a framework for customize it.

HATEOAS 代表 Hypermedia as the Engine of Application State,是 REST 的关键点之一。基本上关键点包括使用资源表示上的链接来映射应用程序状态的有效转换。在这种情况下,服务提供商将提供可通过链接访问的应用程序的有效下一个正确状态。Spring HATEOAS 是 Spring 项目,可帮助在您的资源中构建 Hymeridia 控件。它是一个与 Spring MVC 集成的项目,您可以将其视为构建真正 RESTFull WS 的 Spring MVC 扩展,它非常好地支持将您的服务形式 CRUD(Richardson 模型中的成熟度 2 级)的级别提高到超媒体感知(Richardson 模型中的成熟度级别 3)。另一方面,Spring Data Rest 是一个非常好的项目,它使用 Spring HATEOAS 作为基本砖,为您提供一个可用作 restfull ws 的存储库层。在 proctis 中,该项目有助于减少经典的 boliporlent 代码,以将您的存储库层公开为一个 restfull 端点。我们可以说 tat 是非常不同的项目的提议。使用 Spring HATEOAS,您拥有一个可用于任何类型的 restfull 端点的框架,使用 spring data rest 您拥有一个已经提供端点和自定义框架的 spring 项目。

I hope that this reflections can help you to clarify the difference between the two projects and understand better how use one or the other

我希望这些反思可以帮助您澄清两个项目之间的区别,并更好地理解如何使用一个或另一个