将jpa实体转换为宁静资源的好策略是什么? [英] What is a good strategy for converting jpa entities into restful resources

查看:92
本文介绍了将jpa实体转换为宁静资源的好策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Restful资源并不总是与您的jpa实体进行一对一映射。我看到它有一些问题,我试图找出如何处理:

Restful resources do not always have a one-to-one mapping with your jpa entities. As I see it there are a few problems that I am trying to figure out how to handle:


  1. 当资源有填充的信息时并且由多个实体保存。

  2. 当实体中有更多信息要作为资源发送时。我可以使用杰克逊的 @JsonIgnore 但我仍然会遇到问题1,3和4。

  3. 当一个实体(如聚合体) root)具有嵌套实体,并且您希望包含其嵌套实体的一部分,但仅限于某一级别的嵌套作为您的资源。

  4. 当您想要排除某个实体的一部分时一个父实体的一部分,但当它是不同的父实体的一部分时,它排除一个单独的部分。

  5. Blasted循环引用(我得到的主要是使用 JSOG 使用Jackson的 @JsonIdentityInfo

  1. When a resource has information that is populated and saved by more than one entity.
  2. When an entity has more information in it that you want to send down as a resource. I could just use Jackson's @JsonIgnore but I would still have issue 1, 3 and 4.
  3. When an entity (like an aggregate root) has nested entities and you want to include part of its nested entities but only to a certain level of nesting as your resource.
  4. When you want to exclude once piece of an entity when its part of one parent entity but exclude a separate piece when its part of a different parent entity.
  5. Blasted circular references (I got this mostly working with JSOG using Jackson's @JsonIdentityInfo)

可能的解决方案:
我能想到的唯一方法是处理所有这些问题,就是创建一大堆资源类,将拥有构造函数,这些构造函数使用所需的实体来构造资源,并为其上放置必要的getter和setter。这有点矫枉过正吗?

Possible solutions: The only way I could think of that would handle all of these issues would be to create a whole bunch of "resource" classes that would have constructors that took the needed entities to construct the resource and put necessary getters and setters for that resource on it. Is that overkill?

为了解决2,3,4和5我可以在实际实体上做一些事前和后处理,然后再将它发送给Jackson以序列化或反序列化我的pojo JSON,但这并没有解决问题1.

To solve 2, 3, 4 , and 5 I could just do some pre and post processing on the actual entity before sending it to Jackson to serialize or deserialize my pojo into JSON, but that doesn't address issue 1.

这些都是我认为其他人会遇到的问题,我很好奇其他人提出的解决方案。 (我目前正在使用JPA 2,Spring MVC,Jackson和Spring-Data,但对其他技术开放)

These are all problems I would think others would have come across and I am curious what solutions other people of come up with. (I am currently using JPA 2, Spring MVC, Jackson, and Spring-Data but open to other technologies)

推荐答案

结合使用JAX_RS 1.1和Jackson / GSON,您可以直接将JPA实体作为REST资源公开,但是您会遇到无数问题。

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.

DTO即对JPA实体的预测是要走的路。它允许您将REST的资源表示问题与JPA的事务问题分开。您可以明确定义表示的性质。如果仔细设计DTO /投影,则可以控制表示中显示的数据量,包括要遍历的对象图的深度。您可能需要为同一JPA实体创建多个DTO /预测,以用于可能需要以不同方式表示实体的不同资源。

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.

此外,根据我的使用经验JPA实体上的 @JsonIgnore @JsonIdentityInfo 等注释并不能完全借助更多可用的资源表示。在将对象合并回持久性上下文时(由于忽略属性),最终可能会遇到麻烦,或者您的客户端可能无法使用资源表示,因为可能无法理解作为方案的对象引用。由于缺少标准化,大多数JavaScript客户端通常会在使用 @JsonidentityInfo 注释产生的对象引用时遇到问题。

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.

通过DTO /投影可以实现其他方面。 JPA @EmbeddedId s自然不适合REST资源表示。有些人主张使用JAX-RS @MatrixParam 注释来在资源URI中唯一地标识资源,但对于大多数客户端而言,这不是开箱即用的。矩阵参数毕竟只是一个设计笔记,而不是标准(尚)。使用DTO /投影,您可以根据计算的Id(可能是组成键的组合)提供资源表示。

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).

注意:我目前正在处理适用于REST的JBoss Forge插件,其中存在部分或全部问题,并将通过生成DTO在未来的某个版本中修复。

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.

这篇关于将jpa实体转换为宁静资源的好策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆