使用Jackson将JPA实体序列化为JSON [英] Serializing JPA entities to JSON using Jackson

查看:241
本文介绍了使用Jackson将JPA实体序列化为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Jackson / JPA组合的问题

Question regarding combination of Jackson/JPA


  1. 如果当前应用程序中有大约20个实体,而且我添加了Jackson依赖在POM中,是否意味着默认情况下所有实体都可以转换为 JSON 对象?我看到一个示例项目似乎只有类注释为 @JsonIgnored JSON 跳过。如果是这样,那么怎么会发生这种机制呢?如何处理那些没有任何Jackson注释的实体,默认情况下忽略不了?我一直在网上寻找资源,但运气不大。

  1. If there are about 20 entities in current application and I have add Jackson dependency in POM, does it mean all entities are by default ready to convert to JSON object? I saw a sample project seems only class annotated as @JsonIgnored is skipped by JSON. If so, then how can this happen, what is behind such mechanism? how JACKSON handle those entities which don't have any Jackson annotation, by default ignored or not? I've been looking for resources online but not much luck.

如果20个实体中只有一个需要映射到JSON对象,这是否意味着我必须向所有其他19个实体添加@JsonIgnore?如果没有,杰米逊如何区分实体工作?

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 Jackson differentiate with entity to work on?

谢谢。

推荐答案

杰克逊和JPA与对方没有任何关系。杰克逊是一个JSON解析库,JPA是一个持久性框架。杰克逊可以 几乎任何 对象 - 唯一的要求是对象具有某种可识别的属性( Javabean类型属性,或使用 @JsonProperty 另外还有一个要求,反序列化,目标类型具有默认(无参数)构造函数,所以,例如,这是Jackson可以序列化的对象:

Jackson and JPA don't have anything to do with each other. Jackson is a JSON parsing library and JPA is a persistence framework. Jackson can serialize almost any object - 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 Jackson can serialize:

// 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 ; }
}

这里是另一个:

// Class with a single field annotated with @JsonProperty
class Account {
    @JsonProperty("accountNumber")
    private String accountNumber;
}

这里还有另一个:

@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
}

最后一个例子显示了一个JPA实体类 - 就Jackson而言,它可以像任何其他类型一样被序列化。 但是 ,请注意其字段:当该对象被序列化为JSON时,两个字段将不包括在内 - 密码和地址。这是因为它们已注释为 @JsonIgnore @JsonIgnore 注释允许开发人员说嘿,它可以序列化这个对象,但是当你这样做不包括这些字段在输出。这种排除仅发生在此对象的字段中,因此,例如,如果您在另一个类中包含一个 Address 字段,但不将该字段标记为可忽略,那么这将是序列化。

The last example shows a JPA entity class - as far as Jackson 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 @JsonIgnore annotation 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 Address field in another class, but did not mark the field as ignorable, it would be serialized.

为了防止在任何情况下 类型 的序列化,无论上下文如何,请使用 @JsonIgnoreType 注释。当用于类型时,它基本上意味着我不在乎使用这种类型,不能序列化。

To prevent serialization of a type in all cases, regardless of context, use the @JsonIgnoreType annotation. When used on a type it basically means 'I dont care where this type is used, never serialize it'.

这篇关于使用Jackson将JPA实体序列化为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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