用Jackson从String反序列化LocalDateTime [英] Deserialize LocalDateTime from String with Jackson

查看:462
本文介绍了用Jackson从String反序列化LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson将 String 反序列化为 LocalDateTime ,但这不起作用.

I'm trying to deserialize a String to LocalDateTime with Jackson but it doesn't work.

我有一个带有LocalDateTime字段的数据类:

I have a data class with a LocalDateTime field:

@Data
public class Registration {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime timestamp;
}

我添加了特殊的Jackson数据类型模块:

I added the special Jackson datatype modules:

compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

序列化可以很好地配合:

The serialization works fine with:

new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(registration);

结果字符串:

{"timestamp":"2018-09-03 10:09:35"}

但是反序列化不适用于:

But the deserialization doesn't work with:

new ObjectMapper().registerModule(new JavaTimeModule()).readValue(json.traverse(), Registration.class)

作为错误我得到:

Cannot deserialize value of type `java.time.LocalDateTime` from String "2018-09-03 10:09:35": 
    Failed to deserialize java.time.LocalDateTime: 
        (java.time.format.DateTimeParseException) Text '2018-09-03 10:09:35' could not be parsed: 
            Unable to obtain LocalDateTime from TemporalAccessor: 
                {MinuteOfHour=9, NanoOfSecond=0, SecondOfMinute=35, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=10},
                ISO resolved to 2018-09-03 of type java.time.format.Parsed

我想念什么?我很困惑序列化的工作原理,但是反序列化却行不通.

What am I missing? I'm confused that the serialization works but the deserialization doesn't.

MWE:(小型Gradle Java项目)

MWE: (small gradle Java project)

Main.java:

Main.java:

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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class Main {

    public static void main(String[] args) throws IOException {
        Registration registration = new Registration();
        registration.setTimestamp(LocalDateTime.now());

         ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

         String s = objectMapper.writeValueAsString(registration);
         TreeNode treeNode = objectMapper.readTree(s);

         //Fails here:
         Registration registration1 = objectMapper.readValue(treeNode.traverse(), Registration.class);

        System.out.println(registration1);
    }
}

class Registration {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime timestamp;

    public Registration() {
    }

    public LocalDateTime getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(LocalDateTime localDateTime) {
        this.timestamp = localDateTime;
    }
}

build.gradle:

build.gradle:

plugins {
    id 'java'
}

group 'dateMWE'
version '1.0-SNAPSHOT'

sourceCompatibility = 10

repositories {
    mavenCentral()
}

dependencies {
    compile("com.fasterxml.jackson.core:jackson-annotations:2.9.6")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.6")
}

推荐答案

问题与JSON反序列化无关,而与时间格式字符串有关:

The problem is not related with JSON deserialization, but rather with the time format string:

pattern = "yyyy-MM-dd hh:mm:ss"

请注意,小时设置为 hh :这是一个12小时格式程序,需要"AM"或"PM"值.

Please notice that the hours are set as hh: this is a 12-hour formatter, which requires "AM" or "PM" values.

如果图案更改为

pattern = "yyyy-MM-dd HH:mm:ss"

问题应该解决.

这篇关于用Jackson从String反序列化LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆