简体   繁体   English

Spring HATEOAS中的资源是否可以替代DTO?

[英]Does Resource in Spring HATEOAS replace the DTOs?

I am bulding a REST API in Spring. 我在春季建立了一个REST API。 So until now I have only reading services (GET). 因此,到目前为止,我只有阅读服务(GET)。 For this I used Spring HATEOAS to add links that referring to child elements. 为此,我使用Spring HATEOAS添加指向子元素的链接。

Now I want to add some writing REST-Services. 现在,我想添加一些编写的REST服务。 Usually DTOs are used in REST-Services and those are then mapped to the domain model. 通常,DTO用于REST服务,然后将它们映射到域模型。

So my question is: Can we just use resources from Spring HATEOAS as in the example below and do without DTOs? 所以我的问题是:我们可以像下面的示例那样仅使用Spring HATEOAS的资源而没有DTO吗? Or are resources intended for something else and I still need DTOs? 还是将资源用于其他用途,而我仍然需要DTO?

@PostMapping
public ResponseEntity<String> saveProduct(@RequestBody ProductResource product) {
  ...
}

I would say that Spring HATEOAS doesn't replace DTOs: it builds on top of DTOs. 我会说Spring HATEOAS不会取代DTO:它建立在DTO之上。 So you can make your DTO class extend ResourceSupport or wrap it with Resource<T> . 因此,您可以使DTO类扩展ResourceSupport或将其包装为Resource<T>


The models that represent the domain of your application and the models that represent the data handled by your API are (or at least should be) different concerns and should be decoupled from each other. 代表你的应用的领域 ,代表你的API处理数据模型和模型(或至少应该是) 不同的关注点 ,应该彼此分离 You don't want to break your API clients when you add, remove or rename a field from the application domain model. 在应用程序域模型中添加,删除或重命名字段时,您不想破坏API客户端。

While your service layer operates over the domain/persistence models, your API controllers should operate over a different set of models. 当您的服务层在域/持久性模型上运行时,您的API控制器应在一组不同的模型上运行。 As your domain/persistence models evolve to support new business requirements, for example, you may want to create new versions of the API models to support these changes. 例如,随着域/持久性模型的发展以支持新的业务需求,您可能希望创建API模型的新版本以支持这些更改。 You also may want to deprecate the old versions of your API as new versions are released. 随着新版本的发布,您可能还想弃用旧版本的API。 And it's perfectly possible to achieve when the things are decoupled. 当事物分离时,这是完全有可能实现的。

To minimize the boilerplate code of converting the domain model to the API model (and vice versa), you could rely on frameworks such as MapStruct . 为了最小化将域模型转换为API模型(反之亦然)的样板代码,您可以依赖于诸如MapStruct之类的框架。 And you also could consider using Lombok to generate getters, setters, equals() , hashcode() and toString() methods for you. 您也可以考虑使用Lombok为您生成getter,setter, equals()hashcode()toString()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring HATEOAS不遵守资源链接的默认包含属性 - Spring HATEOAS does not respect default inclusion property for resource link Spring HATEOAS嵌入式资源支持 - Spring HATEOAS embedded resource support 使用 Spring HATEOAS 构建模板化搜索资源 uri - Building a templated search resource uri with Spring HATEOAS 具有资源和资源汇编器的Spring HATEOAS / Jackson - Spring HATEOAS/Jackson with resources and resource assembler Spring HATEOAS构建了分页资源的链接 - Spring HATEOAS build link to paged resource Spring HATEOAS Resource Assembler和Resource Links有很多变量 - Spring HATEOAS Resource Assembler and Resource Links with many variables 使用 spring-boot-starter-hateoas 时找不到 org.springframework.hateoas.Resource 和 org.springframework.hateoas.mvc.ControllerLinkBuilder - org.springframework.hateoas.Resource and org.springframework.hateoas.mvc.ControllerLinkBuilder cannot be found when using spring-boot-starter-hateoas 如何通过Spring Hateoas中的注释设置资源关系? - how to set resource relations through annotations in Spring Hateoas? Spring Hateoas - REST客户端需要模型类+资源类 - Spring Hateoas - REST clients need have model classes + resource classes Spring Hateoas @EnableEntityLinks不提供EntityLinks bean(NoSuchBeanDefinitionException) - Spring Hateoas @EnableEntityLinks does not provide EntityLinks bean (NoSuchBeanDefinitionException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM