简体   繁体   English

无法反序列化`java.time.Instant`类型的值 - jackson

[英]Cannot deserialize value of type `java.time.Instant` - jackson

Having class like this有这样的课

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public final class ActiveRecoveryProcess {

    private UUID recoveryId;
    private Instant startedAt;
}

I'm getting com.fasterxml.jackson.databind.exc.InvalidFormatException with message Cannot deserialize value of type java.time.Instant from String "2020-02-22T16:37:23": Failed to deserialize java.time.Instant: (java.time.format.DateTimeParseException) Text '2020-02-22T16:37:23' could not be parsed at index 19我收到com.fasterxml.jackson.databind.exc.InvalidFormatException消息Cannot deserialize value of type java.time.Instant Cannot deserialize value of typefrom String "2020-02-22T16:37:23": Failed to deserialize java.time.Instant: (java.time.format.DateTimeParseException) Text '2020-02-22T16:37:23' could not be parsed at index 19

JSON input JSON 输入

{"startedAt": "2020-02-22T16:37:23", "recoveryId": "6f6ee3e5-51c7-496a-b845-1c647a64021e"}

Jackson configuration杰克逊配置

    @Autowired
    void configureObjectMapper(final ObjectMapper mapper) {
        mapper.registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule());
        mapper.findAndRegisterModules();
    }

EDIT编辑

JSON is generated from postgres JSON 是从 postgres 生成的

jsonb_build_object(
                        'recoveryId', r.recovery_id,
                        'startedAt', r.started_at
)

where r.started_at is TIMESTAMP.其中r.started_at是时间戳。

One way to do this is to create a Converter .一种方法是创建一个Converter

public final class NoUTCInstant implements Converter<LocalDateTime, Instant> {
    @Override
    public Instant convert(LocalDateTime value) {
        return value.toInstant(ZoneOffset.UTC);
    }
    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return typeFactory.constructType(LocalDateTime.class);
    }
    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return typeFactory.constructType(Instant.class);
    }
}

Then annotate the field.然后注释该字段。

@JsonDeserialize(converter = NoUTCInstant.class)
private Instant startedAt;

The String you're trying to parse, 2020-02-22T16:37:23 , doesn't end in Z .您尝试解析的字符串2020-02-22T16:37:23不以Z结尾。 Instant expects this as it stands for UTC . Instant 期望这一点,因为它代表UTC It simply cannot be parsed.它根本无法解析。 Concat the String with Z to resolve the issue.将字符串与 Z 连接以解决该问题。

        String customInstant = "2020-02-22T16:37:23";

        System.out.println("Instant of: " + Instant.parse(customInstant.concat("Z")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Jackson2JsonRedisSerializer 中反序列化 java.time.Instant - How to deserialize java.time.Instant in Jackson2JsonRedisSerializer 将毫秒时间戳反序列化为java.time.Instant - Deserialize millisecond timestamp to java.time.Instant Spring Boot - Jersey 客户端 - Jackson 无法构造 `java.time.Instant` 的实例 - Spring Boot - Jersey Client - Jackson Cannot construct instance of `java.time.Instant` “com.fasterxml.jackson.databind.JsonMappingException:预期类型为浮点数、整数或字符串。” 使用 ObjectMapper 转换 java.time.Instant - "com.fasterxml.jackson.databind.JsonMappingException: Expected type float, integer, or string." converting the java.time.Instant with ObjectMapper 使用 Jackson ObjectMapper 在 java.time.Instant 和 java.util.Date 之间进行转换 - Use Jackson ObjectMapper to convert between java.time.Instant and java.util.Date 获取错误:日期中的from(java.time.Instant)无法应用于(org.threeten.bp.instant) - Getting error: from(java.time.Instant) in Date cannot be applied to (org.threeten.bp.instant) java.time.Instant(1.8)是线程安全的吗? - java.time.Instant (1.8) is thread safe? 逗号无法通过java.time.Instant解析 - Comma fails parsing by java.time.Instant java.time.Instant 是如何计算纳秒的? - How are nanoseconds calculated in java.time.Instant? Hibernate-在PostgreSQL中使用java.time.Instant - Hibernate - Using java.time.Instant with PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM