With a combination of JAX_RS 1.1 and Jackson/GSON you can expose JPA entities directly as REST resources, but you will run into a myriad of problems.
DTOs i.e. projections onto the JPA entities are the way to go. It would allow you to separate the resource representation concerns of REST from the transactional concerns of JPA. You get to explicitly define the nature of the representations. You can control the amount of data that appears in the representation, including the depth of the object graph to be traversed, if you design your DTOs/projections carefully. You may need to create multiple DTOs/projections for the same JPA entity for the different resources in which the entity may need to be represented differently.
Besides, in my experience using annotations like @JsonIgnore and @JsonIdentityInfo on JPA entities doesnt exactly lend to more usable resource representations. You may eventually run into trouble when merging the objects back into the persistence context (because of ignored properties), or your clients may be unable to consume the resource representations, since object references as a scheme may not be understood. Most JavaScript clients will usually have trouble consuming object references produced by the @JsonidentityInfo annotation, due to the lack of standardization here.
There are other additional aspects that would be possible through DTOs/projections. JPA @EmbeddedIds do not fit naturally into REST resource representations. Some advocate using the JAX-RS @MatrixParam annotation to identify the resource uniquely in the resource URIs, but this does not work out of the box for most clients. Matrix parameters are after all only a design note, and not a standard (yet). With a DTO/projection, you can serve out the resource representation against a computed Id (could be a combination of the constituent keys).
Note: I currently work on the JBoss Forge plugin for REST where some or all of these issues exist and would be fixed in some future release via the generation of DTOs.
1 Separation of layers and clean code. One day you may need to expose the data model using a different format (eg. XML) or interface (eg. non web-service based). Keeping all configuration (such as @JsonIgnore, @JsonidentityInfo) for each interface/format in domain model would make is really messy. DTOs separate the concerns. They can contain all the configuration required by your external interface (web-service) without involving changes in domain model, which can stay web-service and format agnostic.
2 Security - you easily control what is exposed to the client and what the client is allowed to modify.
3 Performance - you easily control what is sent to the client.
4 Issues such as (circular) entity references, lazily-loaded collections are also resolved explicitly and knowingly by you on converting to DTO.
ref: http://stackoverflow.com/questions/17874688/what-is-a-good-strategy-for-converting-jpa-entities-into-restful-resources
分享到:
相关推荐
"A tool for converting the file into pdf format" 指的是一款能够将各种类型的文件转换成PDF格式的工具。这种工具通常支持多种输入格式,如DOC、DOCX、XLS、PPT、TXT、HTML等,以便于分享、打印或长期保存。 PDF...
A Methodology For Converting Polygon Based Standard Cell From Bulk CMOS To SOI CMOS
This is matlab2tikz, a MATLAB(R) script for converting MATLAB figures into native TikZ/Pgfplots figures. matlab2tikz supports the conversion of most MATLAB figures, including 2D and 3D plots. For ...
code for converting gray scale image into color image by using template
这个名为"Converting_text_into_images"的项目是用Delphi编程语言实现的一个小程序,其核心目标是将文本信息转化为不可编辑的图像格式,从而增加了数据的安全性和可移植性。 Delphi是一款强大的面向对象的编程环境...
Open-Source_board_for_converting_RaspberryPI_to_Br_EEGwithRaspberryPI
Flexible JSON serializer for converting between .NET objects and JSON LINQ to JSON for manually reading and writing JSON High performance, faster than .NET"s built-in JSON serializers Write ...
此函数根据由协方差矩阵的特征值的平方根组成的输入计算球面误差可能半径(等效地,来自坐标系中三变量正态分布的 sigma-x、sigma-y 和 sigma-z,其中存在变量之间没有互相关。)这意味着如果您有一个协方差矩阵并...
ool for converting Unity Terrain asset into a mesh. Offers: • Ultra fast converter. • Full vertex count control for generated mesh. • Mesh split system. • 16 and 32 bits mesh support. • ...
描述 "Tools for converting Python code to AWS Step Function json" 提示我们,该工具有助于将Python代码转换成Step Functions所需的JSON定义,这种转换对于构建基于Python的工作流程非常有用。在AWS Step ...
Flexible JSON serializer for converting between .NET objects and JSON LINQ to JSON for manually reading and writing JSON High performance, faster than .NET's built-in JSON serializers Write indented, ...
This is matlab2tikz, a MATLAB(R) script for converting MATLAB figures into native TikZ/Pgfplots figures. matlab2tikz supports the conversion of most MATLAB figures, including 2D and 3D plots.
### 将关系数据库转换为XML文档的关键知识点 随着互联网技术的发展与数据交换需求的增长,XML(可扩展标记语言)作为一种标准化的数据表示与交换格式,在近年来得到了广泛的应用与认可。本文基于一篇研究论文的核心...
Tool for converting Unity Terrain into a mesh. Offers: • Ultra fast converter. • Full vertex count control for generated mesh. • Mesh split system. • 16 and 32 bits mesh support. • Splatmap...
可以使用Decompiler for Kotlin这样的工具,它专门设计用于从字节码恢复Kotlin源代码。 在处理多个类或整个项目时,将字节码文件转换为源代码并打包成.zip文件是有用的。这可以通过编写脚本或者使用自动化工具来...
You’ll find guidelines for converting your product into a native iPhone app using the free PhoneGap framework. And you’ll learn why releasing your product as a web app first helps you find, fix, ...
From converting SQL statements into Elasticsearch queries to a console experience for exploring data using "SELECT x FROM y WHERE z > 1000" type queries, there's a lot on the horizon.
Dive into the world of MVVM, learn how to build modern Windows applications, and prepare for cross-platform development. This book introduces you to the right mindset and demonstrates suitable ...
Allows to specify some options for process of converting. uf_save_ds_as_excel Function which converts datastore into excel file. uf_save_ds_as_excel_parm It is similar to function uf_save_ds_as_...
Develop a methodology for converting legacy applications to the MVVM pattern Architect cross-platform and multi-lingual applications using the MVVM pattern Who This Book Is For Delphi developers with...