java - specification用法 - Spring数据JPA-用于json序列化的ZonedDateTime格式




spring jpa specification用法 (2)

我有ZonedDateTime的json序列化的ZonedDateTime 。 当转换成json时,它会产生一个巨大的对象,我不希望每次都传输所有的数据。 所以我试图将其格式化为ISO,但它不起作用。 我怎样才能把它格式化?

这是我的实体类:

@MappedSuperclass
public abstract class AuditBase {

    @Id
    @GeneratedValue
    private Long id;

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    public ZonedDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(ZonedDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @PrePersist
    public void prePersist() {
        this.createdDate = ZonedDateTime.now();
        this.lastModifiedDate = ZonedDateTime.now();
    }

    @PreUpdate
    public void preUpdate() {
        this.lastModifiedDate = ZonedDateTime.now();
    }
}

我猜你正在使用Jackson进行json序列化,Jackson现在有一个用于Java 8新日期时间API的模块, https://github.com/FasterXML/jackson-datatype-jsr310

把这个依赖关系添加到你的pom.xml中

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

这是它的用法:

 public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    System.out.println(objectMapper.writeValueAsString(new Entity()));
}

static class Entity {
    ZonedDateTime time = ZonedDateTime.now();

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    public ZonedDateTime getTime() {
        return time;
    }
}

输出是:

{"time":"2015-07-25T23:09:01.795+0700"}

注意:如果您的杰克逊版本是2.4.x使用

objectMapper.registerModule(new JSR310Module());

希望这可以帮助!


以上答案的作品,但如果你不想触摸你现有的实体类,以下设置将使用ZonedDateTime

  public static ObjectMapper getMapper() {
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.registerModule(new JavaTimeModule());    
    return mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,false)
                        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false)
                        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                        .setVisibility(mapper.getSerializationConfig()
                                .getDefaultVisibilityChecker()
                                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); 
    }

图书馆:

<dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-csv</artifactId>
            <version>${jackson.dataformat.csv.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>




spring-jpa