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

查看:26
本文介绍了将 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. 当实体中有更多信息要作为资源发送时.我可以只使用 Jackson 的 @JsonIgnore,但我仍然会遇到问题 1、3 和 4.
  3. 当实体(如聚合根)具有嵌套实体并且您希望包含其嵌套实体的一部分但仅包含特定级别的嵌套作为您的资源时.
  4. 当您希望在实体属于一个父实体时排除该实体的一部分,但在其属于不同父实体时排除单独的一部分.
  5. Blasted 循环引用(我主要使用 Jackson 的 @JsonIdentityInfo 与 JSOG 一起工作)代码>)
  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 @EmbeddedIds 不适合 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天全站免登陆