Jackson JSON ObjectMapper 添加一个 'new' 属性

Jackson JSON ObjectMapper adding a 'new' property

我正在为我的应用程序试用 Jackson XML ObjectMapper。

我写了这个小class。所以最后它打印回反序列化的对象。这没关系,除了每个级别的对象它打印一个新的 属性 称为 "new": true.

我不确定它来自哪里。该对象是一个 spring jpa entity.Is 这个 属性 Spring 引入的东西?它绝对不是 class 上的 属性,因为在删除新的 属性 之前我无法读回对象字符串。 例如

lastModifiedDate":null,
"createdBy":null,
"new":true
},

这是代码:

/**
* Updates the layout from a JSON String in the format for the layout definition file
* @return
*/
public Layout updateFromJSONString(String jsonString)
{
    logger.entry(jsonString);

    Layout layout = null;
    try {
        layout = new ObjectMapper().readValue(jsonString, Layout.class);
        logger.exit(new ObjectMapper().writeValueAsString(layout));

    } catch (IOException e) {
        throw new RuntimeException(e)
    }


    return layout;
}

以及可能导致问题的基础实体 - 还有 2 个其他子class 具有简单的属性,包括嵌套对象在内的所有对象都继承自该基础 class:

public class AbstractAuditableEntity extends AbstractPersistable<Long> implements Auditable<SystemUser, Long> {

    @Column(length = 30)
    private String externalIdentifier;

    @Basic
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @org.springframework.format.annotation.DateTimeFormat(pattern = DateConstants.DEFAULT_TIME_FORMAT)
    private DateTime lastUpdated;

    @OneToOne(fetch = FetchType.LAZY)
    private SystemUser lastUpdateUser;

    @Basic
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @org.springframework.format.annotation.DateTimeFormat(pattern = DateConstants.DEFAULT_TIME_FORMAT)
    private DateTime created;

    @OneToOne(fetch = FetchType.LAZY)
    private SystemUser createUser;



    /**
     * Gets created by audit user.
     */
    @Override
    public SystemUser getCreatedBy() {
        return createUser;
    }

    /**
     * Sets created by audit user.
     */
    @Override

    public void setCreatedBy(SystemUser createdBy) {
        this.createUser = createdBy;
    }

    /**
     * Gets create audit date.
     */
    public DateTime getCreated() {
        return created;
    }

    /**
     * Sets create audit date.
     */
    public void setCreated(DateTime creationDate) {
        this.created = creationDate;
    }

    /**
     * Gets last modified by audit user.
     */
    public SystemUser getLastUpdateUser() {
        return lastUpdateUser;
    }

    /**
     * Sets last modified by audit user.
     */
    public void setLastUpdateUser(SystemUser lastUpdateUser) {
        this.lastUpdateUser = lastUpdateUser;
    }

    /**
     * Gets last modified audit date.
     */
    public DateTime getLastUpdated() {
        return lastUpdated;
    }

    /**
     * Sets last modified audit date.
     */
    public void setLastUpdated(DateTime lastModifiedDate) {
        this.lastUpdated = lastModifiedDate;
    }

    @Override
    public DateTime getCreatedDate() {
        return lastUpdated;
    }

    @Override
    public void setCreatedDate(DateTime dateTime) {
        setCreated(dateTime);

    }

    @Override
    public SystemUser getLastModifiedBy() {
        return lastUpdateUser;
    }

    @Override
    public void setLastModifiedBy(SystemUser systemUser) {
        lastUpdateUser = systemUser;

    }

    @Override
    public DateTime getLastModifiedDate() {
        return lastUpdated;
    }

    @Override
    public void setLastModifiedDate(DateTime dateTime) {
        lastUpdated = dateTime;

    }

    public String getExternalIdentifier() {
        return externalIdentifier;
    }

    public void setExternalIdentifier(String externalIdentifier) {
        this.externalIdentifier = externalIdentifier;
    }

    public void setId(Long id)
    {
        super.setId(id);
    }

    public Long getId()
    {
        return super.getId();
    }
}

看看 javadoc of AbstractPersistable:它有一个返回布尔值的方法 isNew()。这就是杰克逊在您的 JSON.

中放置 new 属性的原因

您可以通过重写 isNew() 方法并使用 @JsonIgnore.

对其进行注释,从而轻松实现此 属性