java Spring Boot 中 OffsetDateTime 的 Jackson 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41876037/
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
Hymanson date-format for OffsetDateTime in Spring Boot
提问by Dave
I'm trying to output an OffsetDateTime from my Spring application, and have in my application.properties these properties:
我试图从我的 Spring 应用程序输出一个 OffsetDateTime,并在我的 application.properties 中有这些属性:
spring.Hymanson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring.Hymanson.date-format=yyyy-MM-dd'T'HH:mm
However when the date is returned it is formatted as
但是,当返回日期时,它的格式为
"2017-01-30T16:55:00Z"
How should I correctly configure the format for the date in my Spring application?
我应该如何正确配置 Spring 应用程序中的日期格式?
采纳答案by Dave
So I've managed to figure out a solution, but if you have an alternative please post it.
所以我设法找到了一个解决方案,但如果你有替代方案,请发布它。
I ended up creating a new primary ObjectMapper
bean, and registering a new module with a custom serializer for OffsetDateTime
. I'm able to set my own date format in here, using java.time.format.DateTimeFormatter
. I also had to register the JavaTimeModule
with my mapper.
我最终创建了一个新的主ObjectMapper
bean,并使用自定义序列化程序为OffsetDateTime
. 我可以在这里设置我自己的日期格式,使用java.time.format.DateTimeFormatter
. 我还必须向JavaTimeModule
我的映射器注册。
@Configuration
public class HymansonOffsetDateTimeMapper{
@Primary
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
@Override
public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(offsetDateTime));
}
});
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}
回答by danny
The spring property doesn't work for me as well. Setting the property to ObjectMapper
works for me though.
spring 属性也不适用于我。不过,将属性设置ObjectMapper
为对我有用。
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
回答by LeslieM
Adding a dependency on Hymanson-modules-java8 worked for me (Hymanson-datatype-jsr310 is deprecated)
添加对 Hymanson-modules-java8 的依赖对我有用(不推荐使用 Hymanson-datatype-jsr310)
<!-- deserialize Java 8 date time types e.g OffsetDateTime -->
<dependency>
<groupId>com.fasterxml.Hymanson.module</groupId>
<artifactId>Hymanson-modules-java8</artifactId>
</dependency>
I also needed to add this for it to work:
我还需要添加它以使其工作:
om.registerModule(new JavaTimeModule());
No need for the write-dates-as-timestamps=false or om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - that is applicable for Java "Date" object.
不需要 write-dates-as-timestamps=false 或 om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - 适用于 Java“日期”对象。
I used this annotation:
我使用了这个注释:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
and get output like this:
并得到这样的输出:
"timestamp":"2020-04-23T08:00:00.000-06:00"
“时间戳”:“2020-04-23T08:00:00.000-06:00”
回答by Konstantin Pavlov
- Add Hymanson-datatype-jsr310 to your dependencies
Add to application.properties:
spring.Hymanson.serialization.write-dates-as-timestamps=false
- 将 Hymanson-datatype-jsr310 添加到您的依赖项
添加到 application.properties:
spring.Hymanson.serialization.write-dates-as-timestamps=false
You will get:
你会得到:
"lastUpdated": "2017-07-16T19:17:57.689Z"
回答by WesternGun
Have you tried put @JsonFormat(pattern="dd/MM/yyyy HH:mm:ss Z")
before your field?
你有没有试过放在@JsonFormat(pattern="dd/MM/yyyy HH:mm:ss Z")
你的领域之前?
@JsonProperty("timestamp")
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
private OffsetDateTime timestamp;
I guess you will get:
我想你会得到:
2017-01-30'T'16:55