gpt4 book ai didi

java - Spring 启动 + jackson + LocalDateTime : Date not parsed correctly

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:54 27 4
gpt4 key购买 nike

我的实体类中有一个 LocalDateTime 属性,但是当它被序列化时,我没有看到预期的格式。

这是类:

public class MyEntity {

private Integer id;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private LocalDateTime changeDate;

// getters and setters

}

jackson 是这样格式化它的:

{
"id": 56,
"changeDate": {
"hour":14, "minute":19,
"nano":797000000,
"second":7,
"dayOfMonth":24,
"dayOfWeek":"TUESDAY",
"dayOfYear":297, "month":"OCTOBER",
"monthValue":10,
"year":2017,
"chronology": {
"id":"ISO",
"calendarType":"iso8601"
}
}
}

请注意,我向我的 pom 添加了以下依赖项:

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

我不包括版本,因为 spring boot 会处理它。顺便说一下,我正在使用 spring boot 1.5.2.RELEASE。

我还在 application.properties 中包含了以下属性:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

知道为什么日期不是这样格式化的,而是使用我提供的模式吗?

最佳答案

刚刚检查了我的项目。我的反序列化器:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public class JsonDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

private static final String NULL_VALUE = "null";

@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
String dateString = node.textValue();

LocalDateTime dateTime = null;
if (!NULL_VALUE.equals(dateString)) {
dateTime = LocalDateTime.parse(dateString, ISO_LOCAL_DATE_TIME);
}
return dateTime;
}
}

我的序列化器:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public class JsonLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator, SerializerProvider provider)
throws IOException {

String dateTimeString = dateTime.format(ISO_LOCAL_DATE_TIME);
generator.writeString(dateTimeString);
}
}

您需要设置自己的格式化程序。

在我的 RestConfig 中我有:

@Configuration
@ComponentScan(value = {"ru.outofrange.controller"})
public class RestConfig extends RepositoryRestConfigurerAdapter {

@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

registerSerializerDeserializer(objectMapper);
}

private void registerSerializerDeserializer(ObjectMapper objectMapper) {
SimpleModule module = new SimpleModule();

module.addSerializer(LocalDateTime.class, new JsonLocalDateTimeSerializer());
module.addDeserializer(LocalDateTime.class, new JsonDateTimeDeserializer());

objectMapper.registerModule(module);
}

}

关于java - Spring 启动 + jackson + LocalDateTime : Date not parsed correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com