jackson 自动转换为LocalDateTime

64jmpszr  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(162)

我有一个请求体类,看起来像这样:

import com.fasterxml.jackson.annotation.JsonFormat;
import io.micronaut.serde.annotation.Serdeable;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@Serdeable
public class CheckDto {

    ...

    @NotNull
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime startTime;
}

字符串
如果我用Jackson和JSR 310手动实现它,它工作得很好。如果我把它留给Jesaut重新实现Jackson,它会失败(当它到达空格时似乎会中断)。我可以用一个测试类重复它:

import io.micronaut.serde.ObjectMapper;
import mypackage.CheckDto;

import java.io.IOException;

public class Test {

    private static final ObjectMapper mapper = ObjectMapper.getDefault();

    public static void main(String[] args) throws IOException {
        String json = "{\"startTime\":\"2023-09-28 17:00\"}";
        CheckDto dto = mapper.readValue(json, CheckDto.class);

        System.out.println(dto.getStartTime());
    }
}


错误代码:

Exception in thread "main" io.micronaut.serde.exceptions.SerdeException: Error decoding property [LocalDateTime startTime] of type [class mypackage.CheckDto]: Text '2023-09-28 17:00' could not be parsed at index 10
    at io.micronaut.serde.support.deserializers.DeserBean$DerProperty.deserializeAndSetPropertyValue(DeserBean.java:825)
    at io.micronaut.serde.support.deserializers.SimpleObjectDeserializer.deserializeInto(SimpleObjectDeserializer.java:96)
    at io.micronaut.serde.support.deserializers.SimpleObjectDeserializer.deserialize(SimpleObjectDeserializer.java:68)
    at io.micronaut.serde.support.deserializers.SimpleObjectDeserializer.deserializeNullable(SimpleObjectDeserializer.java:78)
    at io.micronaut.serde.jackson.JacksonJsonMapper.readValue0(JacksonJsonMapper.java:130)
    at io.micronaut.serde.jackson.JacksonJsonMapper.readValue(JacksonJsonMapper.java:122)
    at io.micronaut.serde.jackson.JacksonJsonMapper.readValue(JacksonJsonMapper.java:168)
    at io.micronaut.json.JsonMapper.readValue(JsonMapper.java:141)
    at io.micronaut.json.JsonMapper.readValue(JsonMapper.java:168)
    at Test.main(Test.java:12)
Caused by: java.time.format.DateTimeParseException: Text '2023-09-28 17:00' could not be parsed at index 10
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2106)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:2008)
    at io.micronaut.serde.support.serdes.DefaultFormattedTemporalSerde.deserialize(DefaultFormattedTemporalSerde.java:71)
    at io.micronaut.serde.support.serdes.DefaultFormattedTemporalSerde.deserialize(DefaultFormattedTemporalSerde.java:35)
    at io.micronaut.serde.Deserializer.deserializeNullable(Deserializer.java:85)
    at io.micronaut.serde.support.deserializers.DeserBean$DerProperty.deserializeAndSetPropertyValue(DeserBean.java:806)
    ... 9 more


有什么建议吗?谢谢。
埃塔:也许我应该指出,我 * 认为 * 我有这个工作下,我已经升级到4最近。

kuhbmx9i

kuhbmx9i1#

注解处理器microaut-serde-processor没有运行,这就是原因。
(我把我的dtos移到了一个单独的jar文件/ maven模块中,从一个在pom中配置了注解处理器的模块移到了一个没有配置的模块)。

相关问题