杰克逊没有序列化版本字段

时间:2017-06-22 09:10:47

标签: java json spring hibernate jackson

我正在使用带有Spring Data Rest和JPA的Spring boot 1.5.3。我遇到了一个非常奇怪的问题。

这是我的实体bean的超类:

@EntityListeners({ AuditingEntityListener.class })
@MappedSuperclass
@Audited
public abstract class AbstractEntity extends AbstractPersistable<Long> {
    private static final long serialVersionUID = 1L;

    /* "UUID" and "UID" are Oracle reserved keywords -> "sid" */
    @Column(name = "sid", unique = true, nullable = false, updatable = false, length = 36)
    private String sid;

    @CreatedBy
    private String createdBy;

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime lastModifiedDate;

    @LastModifiedBy
    private String lastModifiedBy;

    // Trick to start version counting from 1 instead of 0
    @Version
    private int version = 1;

    public AbstractEntity() {
    }

    @PrePersist
    public void initializeUUID() {
        if (sid == null) {
            sid = UUID.randomUUID().toString();
        }
    }

    public String getSid() {
        return sid;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public int getVersion() {
        return version;
    }

    @Override
    public boolean equals(Object o) {
        return (o == this || (o instanceof AbstractEntity && sid.equals(((AbstractEntity) o).sid)));
    }

    @Override
    public int hashCode() {
        return sid.hashCode();
    }

}

这是我坚持的实体:

@Entity
public class Country extends AbstractEntity {
    private static final long serialVersionUID = 4487829062553023011L;

    @Audited
    @NotNull(message = "The name cannot be empty")
    @Column(nullable = false)
    private String name;

    @Audited
    @NotNull(message = "The ISO code cannot be empty")
    @Size(min = 2, max = 2)
    @Column(nullable = false, unique = true)
    private String isoCode;

    public Country() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

}

当我看到Swagger时,我看到了模型的正确定义:

Country {
    createdDate (string, optional),
    id (integer, optional),
    isoCode (string),
    lastModifiedBy (string, optional),
    lastModifiedDate (string, optional),
    name (string),
    new (boolean, optional),
    sid (string, optional),
    version (integer, optional)
}

但是,当我要求所有国家/地区或特定国家/地区时,版本字段不可见:

{
  "sid": "cd2527a0-4667-4d25-a098-70f12bfed2da",
  "createdDate": "2017-06-22T11:02:48",
  "lastModifiedDate": "2017-06-22T11:02:48",
  "lastModifiedBy": null,
  "name": "Italy",
  "isoCode": "IT",
  "new": false,
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/v1/countries/1"
    },
    "country": {
      "href": "http://localhost:8080/api/v1/countries/1"
    }
  }

}

我认为这是保留关键字的问题,但事实并非如此。同时重命名字段,它存在于模型中,但不存在于返回数据中。

我做错了吗?我需要这个版本,因为Hibernate需要它才能正确持久。

0 个答案:

没有答案