OffsetDateTime jsonFormat 允许两种类型的模式
OffsetDateTime jsonFormat allowing two type of patterns
我正在 Spring Boot 中实现应用程序。使用杰克逊。
是否有指定 JsonFormat 来解析两种类型的日期,例如:
2020-10-06T10:15:30+01:00
2020-10-06T10:15:30
我的当前领域:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss Z")
private OffsetDateTime interval;
我是否必须在配置中的某处指定一些 ObjectMapper?
我正在检索此错误:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-10-06T10:15:30+01:00": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-10-06T10:15:30+01:00' could not be parsed at index 10
此致
您需要使用带可选区域偏移的 DateTimeFormatter
进行解析,因此您需要自定义 JsonDeserializer
:
public class OffsetDateTimeOptionalZoneDeserializer extends StdScalarDeserializer<OffsetDateTime> {
private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffsetId()
.optionalEnd()
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.toFormatter();
public OffsetDateTimeOptionalZoneDeserializer() { super(OffsetDateTime.class); }
@Override
public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return OffsetDateTime.parse(p.getText(), formatter);
}
}
测试
public class Test {
@JsonDeserialize(using = OffsetDateTimeOptionalZoneDeserializer.class)
private OffsetDateTime interval;
public static void main(String[] args) throws Exception {
String json = "[" +
"{ \"interval\": \"2020-10-06T10:15:30+01:00\" }," +
"{ \"interval\": \"2020-10-06T10:15:30Z\" }," +
"{ \"interval\": \"2020-10-06T10:15:30\" }" +
"]";
ObjectMapper mapper = new ObjectMapper();
Test[] tests = mapper.readValue(json, Test[].class);
for (Test t : tests)
System.out.println(t.interval);
}
}
输出
2020-10-06T10:15:30+01:00
2020-10-06T10:15:30Z
2020-10-06T10:15:30Z
我正在 Spring Boot 中实现应用程序。使用杰克逊。
是否有指定 JsonFormat 来解析两种类型的日期,例如:
2020-10-06T10:15:30+01:00
2020-10-06T10:15:30
我的当前领域:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss Z")
private OffsetDateTime interval;
我是否必须在配置中的某处指定一些 ObjectMapper?
我正在检索此错误:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-10-06T10:15:30+01:00": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-10-06T10:15:30+01:00' could not be parsed at index 10
此致
您需要使用带可选区域偏移的 DateTimeFormatter
进行解析,因此您需要自定义 JsonDeserializer
:
public class OffsetDateTimeOptionalZoneDeserializer extends StdScalarDeserializer<OffsetDateTime> {
private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffsetId()
.optionalEnd()
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.toFormatter();
public OffsetDateTimeOptionalZoneDeserializer() { super(OffsetDateTime.class); }
@Override
public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return OffsetDateTime.parse(p.getText(), formatter);
}
}
测试
public class Test {
@JsonDeserialize(using = OffsetDateTimeOptionalZoneDeserializer.class)
private OffsetDateTime interval;
public static void main(String[] args) throws Exception {
String json = "[" +
"{ \"interval\": \"2020-10-06T10:15:30+01:00\" }," +
"{ \"interval\": \"2020-10-06T10:15:30Z\" }," +
"{ \"interval\": \"2020-10-06T10:15:30\" }" +
"]";
ObjectMapper mapper = new ObjectMapper();
Test[] tests = mapper.readValue(json, Test[].class);
for (Test t : tests)
System.out.println(t.interval);
}
}
输出
2020-10-06T10:15:30+01:00
2020-10-06T10:15:30Z
2020-10-06T10:15:30Z