java - JSON反序列化问题-解析Instant时出错

标签 java azure date

我需要将对象保留在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/

相关文章:

Java读取文件并写入数组

c# - Azure Web 作业中的 LocalReport 呈现

python - df = pd.read_csv ('iris.csv' ) 指向 azure blob 报告中的文件 [errno 2] 没有这样的文件或目录

grails - 比较两个Groovy日期

java - 如何测量@Scheduled cron 的周期?

java - Spring RestTemplate 抛出异常 "Broken pipe",同时调用不同的 Rest API 同步

node.js - 使用 ngrok 和 Bot 框架未找到 404

Java:跨越数月的两个日期之间的差异

php - 如何检查日期是否在今天前三天

java - 西蒙说按下按钮后按钮颜色不会恢复正常