我需要将对象保留在Azure 表存储中。问题是我不想在实体类中使用日期类型。我想使用 Instant,但在解析 JasonMappingException
时遇到问题。
这是我的实体类:
@Data
public class Event extends TableServiceEntity {
@NotNull
@JsonDeserialize(using = JsonDateDeserializer.class)
private Instant eventStart;
@NotNull
@JsonDeserialize(using = JsonDateDeserializer.class)
private Instant eventEnd;
@NotBlank
private String eventName;
private String eventDescription;
//default constructor
public Event () {}
}
这是我的反序列化器:
public class JsonDateDeserializer extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
String date = jsonParser.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
return sdf.parse(date).toInstant();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
我收到此错误:
2020-06-10 20:47:19.685 WARN 11745 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: java.text.ParseException: Unparseable date: "2020-06-12T08:00:00UTC"; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.text.ParseException: Unparseable date: "2020-06-12T08:00:00UTC" (through reference chain: com.komix.eventmanager.model.Event["eventStart"])]
我厌倦了 Azure DB 的日期问题。你能帮我一下吗?
编辑:好的,经过一些编辑解析没问题,但应用程序给了我以下错误:
java.lang.IllegalArgumentException:不支持类型类 java.time.Instant。
Azure表存储好像不支持Instant?!我什至找不到任何有关支持的 Asure Table 数据类型的好的文档...
最佳答案
'Z'
在这里被视为常量。您需要传递不带引号的 z
。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
文档引用 here
关于java - JSON反序列化问题-解析Instant时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62311218/