使用 Jackson 将 JPA 实体序列化为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15975363/
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
Serializing JPA entities to JSON using Hymanson
提问by Dreamer
Question regarding combination of Hymanson/JPA
关于Hyman逊/JPA组合的问题
If there are about 20 entities in current application and I have add Hymanson dependency in POM, does it mean all entities are by default ready to convert to
JSONobject? I saw a sample project seems only class annotated as@JsonIgnoredis skipped byJSON. If so, then how can this happen, what is behind such mechanism? howHymanSONhandle those entities which don't have any Hymanson annotation, by default ignored or not? I've been looking for resources online but not much luck.If only one of the 20 entities need to be mapped to JSON object, does it mean I have to add @JsonIgnore to all other 19 entities? If not, how
Hymansondifferentiate with entity to work on?
如果当前应用程序中有大约 20 个实体并且我在 POM 中添加了 Hymanson 依赖项,这是否意味着默认情况下所有实体都准备好转换为
JSON对象?我看到一个示例项目似乎只有被注释的类@JsonIgnored被跳过JSON。如果是这样,那么这怎么会发生,这种机制的背后是什么?如何HymanSON处理那些没有任何 Hymanson 注释的实体,默认情况下是否忽略?我一直在网上寻找资源,但运气不佳。如果 20 个实体中只有一个需要映射到 JSON 对象,是否意味着我必须将 @JsonIgnore 添加到所有其他 19 个实体?如果不是,如何
Hymanson与要处理的实体区分开来?
Thanks.
谢谢。
回答by Perception
Hymanson and JPA don't have anything to do with each other. Hymanson is a JSON parsing library and JPA is a persistence framework. Hymanson can serializealmost anyobject - the only requirement being that the object have some kind of recognizable properties (Javabean type properties, or bare fields annotated with @JsonProperty. There is an additional requirement for deserialization, that the target type have a default (no-arg) constructor. So, for example, this is an object that Hymanson can serialize:
Hymanson 和 JPA 彼此没有任何关系。Hymanson 是一个 JSON 解析库,JPA 是一个持久化框架。Hymanson几乎可以序列化任何对象——唯一的要求是该对象具有某种可识别的属性(Javabean 类型属性,或用 注释的裸字段@JsonProperty。反序列化还有一个额外要求,即目标类型具有默认值(无参数) ) 构造函数。例如,这是一个 Hymanson 可以序列化的对象:
// Class with a single Javabean property, "name"
class Person {
private String name;
public String getName() { return name ; }
public String setName(String name) { this.name = name ; }
}
And here is another:
这是另一个:
// Class with a single field annotated with @JsonProperty
class Account {
@JsonProperty("accountNumber")
private String accountNumber;
}
And here is yet another:
这是另一个:
@Entity
public class User {
@Id
private Long id;
@Basic
private String userName;
@Basic
@JsonIgnore
private String password;
@Basic
@JsonIgnore
private Address address;
// Constructors, getters, setters
}
The last example shows a JPA entity class - as far as Hymanson is concerned it can be serialized just like any other type. But, take note of its fields: when this object is serialized into JSON two of the fields will not be included - 'password' and 'address'. This is because they have been annotated with @JsonIgnore. The @JsonIgnoreannotation allows a developer to say 'Hey, its ok to serialize this object, but when you do so don't include these fields in the output'. This exclusion only occurs for the fields of this object, so for example, if you included an Addressfield in another class, but did not mark the field as ignorable, it would be serialized.
最后一个示例显示了一个 JPA 实体类 - 就 Hymanson 而言,它可以像任何其他类型一样被序列化。但是,请注意它的字段:当这个对象被序列化为 JSON 时,其中两个字段将不会被包含 - 'password' 和 'address'。这是因为它们已被注释@JsonIgnore。该@JsonIgnore注释允许开发人员说:“嘿,其确定序列化这个对象,但是当你这样做不包括在输出这些领域”。此排除仅针对此对象的字段发生,因此,例如,如果您Address在另一个类中包含一个字段,但未将该字段标记为可忽略,它将被序列化。
To prevent serialization of a type in all cases, regardless of context, use the @JsonIgnoreTypeannotation. When used on a type it basically means 'I dont care where this type is used, never serialize it'.
为了防止在所有情况下序列化类型,无论上下文如何,请使用@JsonIgnoreType注释。当在类型上使用时,它基本上意味着“我不在乎在哪里使用这种类型,从不序列化它”。
回答by Pascal Gélinas
No, you don't need to add @JsonIgnoreon every class and if you had tried you would have gotten a compile error, since you can't put it there. Hymanson will only work on objects you give to it, it's no magic.
不,您不需要添加@JsonIgnore每个类,如果您尝试过,您会得到编译错误,因为您不能将它放在那里。Hyman逊只会处理你给它的东西,这不是魔术。
The Hymanson documentation is easily found online, such at its project page on githubor on the codehaus website.

