Java Spring Data JPA - 用于 json 序列化的 ZonedDateTime 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31627992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA - ZonedDateTime format for json serialization
提问by Christos Baziotis
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?
我对 .json 的 json 序列化有问题ZonedDateTime
。当转换为 json 时,它会产生一个巨大的对象,我不希望每次都传输所有这些数据。所以我试图将其格式化为 ISO,但它不起作用。我怎样才能让它格式化?
Here is my Entity Class:
这是我的实体类:
@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();
}
}
采纳答案by toandv
I guess that you are using Hymanson for json serialization, Hymanson now has a module for Java 8 new date time API, https://github.com/FasterXML/Hymanson-datatype-jsr310.
我猜您正在使用 Hymanson 进行 json 序列化,Hymanson 现在有一个用于 Java 8 新日期时间 API 的模块,https://github.com/FasterXML/Hymanson-datatype-jsr310。
Add this dependency into your pom.xml
将此依赖项添加到您的 pom.xml 中
<dependency>
<groupId>com.fasterxml.Hymanson.datatype</groupId>
<artifactId>Hymanson-datatype-jsr310</artifactId>
<version>2.6.0</version>
</dependency>
And this is its usage:
这是它的用法:
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;
}
}
The output is:
输出是:
{"time":"2015-07-25T23:09:01.795+0700"}
Note : If your Hymanson version is 2.4.x use
注意:如果您的 Hymanson 版本是 2.4.x,请使用
objectMapper.registerModule(new JSR310Module());
Hope this helps!
希望这可以帮助!
回答by Gurkha
Above answer works but if you don't want to touch your existing entity class, following settings will work with ZonedDateTime
:
以上答案有效,但如果您不想触及现有实体类,以下设置将适用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));
}
Library:
图书馆:
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-dataformat-csv</artifactId>
<version>${Hymanson.dataformat.csv.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.datatype</groupId>
<artifactId>Hymanson-datatype-jsr310</artifactId>
</dependency>