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

时间:2015-01-23 13:17:39

标签: spring-mvc jackson

我只是为我的应用程序试用Jackson XML ObjectMapper。

我写了这个小班。所以最后它打印出反序列化的对象。哪个没关系,除了每个级别的对象,它都会打印一个名为" new":true的新属性。

我不知道它来自哪里。该对象是一个spring jpa实体。这个属性是Spring引入的吗?它绝对不是课堂上的属性,因为在删除新属性之前我无法读取对象字符串。 e.g

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个其他只有简单属性的子类,包括嵌套对象在内的所有对象都继承自这个基类:

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();
    }
}

1 个答案:

答案 0 :(得分:1)

查看javadoc of AbstractPersistable:它有一个方法isNew()返回一个布尔值。这就是杰克逊在你的JSON中放置new属性的原因。

您可以通过覆盖isNew()方法并使用@JsonIgnore对其进行注释来轻松获取此属性。