Spring Data JPA - 用于json序列化的ZonedDateTime格式 [英] Spring Data JPA - ZonedDateTime format for json serialization

查看:1813
本文介绍了Spring Data JPA - 用于json序列化的ZonedDateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 ZonedDateTime 的json序列化问题。当转换为json时,它产生了一个巨大的对象,我不希望每次都传输所有数据。所以我试着将其格式化为ISO,但它不起作用。如何才能格式化?

I have a problem with the json serialization of ZonedDateTime. When converted to json it produces an enormous object and I don't want all that data to be transfered every time. So i tried to format it to ISO but it doesn't work. How can i get it to format?

这是我的实体类:

@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

I guess that you are using Jackson for json serialization, Jackson now has a module for Java 8 new date time API, https://github.com/FasterXML/jackson-datatype-jsr310.

将此依赖项添加到pom.xml中

Add this dependency into your 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"}

注意:如果您的Jackson版本是2.4.x,请使用

Note : If your Jackson version is 2.4.x use

objectMapper.registerModule(new JSR310Module());

希望这有帮助!

这篇关于Spring Data JPA - 用于json序列化的ZonedDateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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