简体   繁体   English

无法从 String 反序列化 java.time.LocalDateTime 类型的值

[英]Can not deserialize value of type java.time.LocalDateTime from String

I have following configuration:我有以下配置:

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//        objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
        return objectMapper;
    }

and following dependencies:以及以下依赖项:

ext {
        springBootVersion = '1.5.2.RELEASE'
    }
.... 
dependencies {
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile("org.springframework:spring-messaging")
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
    compile ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

I added the following controller:我添加了以下控制器:

@PostMapping("/validation_test")
    public String testValidation(@Valid @RequestBody ClientInputMessage clientInputMessage, BindingResult result) {
        logger.info(Arrays.toString(result.getAllErrors().toArray()));
        return "main";
    }


public class ClientInputMessage {
    @NotEmpty
    private String num1;
    @NotEmpty
    private String num2;
    @Past
    private LocalDateTime date;

If I pass json like this:如果我像这样传递json:

{
      "num1":"324",
      "num2":123,
      "date":"2014-01-01"
    }

application prints following output:应用程序打印以下输出:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10
 at [Source: java.io.PushbackInputStream@1204f40f; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10
 at [Source: java.io.PushbackInputStream@1204f40f; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"])

Original answer:原答案:

LocalDateTime in java does not accept "2014-01-01" as a valid date string. Java 中的 LocalDateTime 不接受“2014-01-01”作为有效的日期字符串。

Some additional info:一些额外的信息:

If you don't actually care what type your date is (LocalDate, OffsetDate, ZonedDate, ...), you can make it a TemporalAccessor , then use DateTimeFormatter::parseBest to parse the date.如果您实际上并不关心您的日期是什么类型(LocalDate、OffsetDate、ZonedDate,...),您可以将其设为TemporalAccessor ,然后使用DateTimeFormatter::parseBest来解析日期。

PS聚苯乙烯
string "2014-01-01T00:00:00" will be valid for LocalDateTime字符串 "2014-01-01T00:00:00" 对 LocalDateTime 有效

You can simply tell the Deserializer that what comes should be a LocalDate even if you have a LocalDateTime in output, having care to have an alternative setter for your LocalDate variant .你可以简单地告诉解串器是什么来应该是一个LocalDate ,即使你有一个LocalDateTime输出,有照顾有你的一个替代的setter LocalDate变种

Something as:东西如:

@JsonDeserialize(as = LocalDate.class)
@Past
private LocalDateTime date;

public void setDate(LocalDateTime input) {
    date = input;
}

public void setDate(LocalDate input) {
    date = input.atStartOfDay();
}

暂无
暂无

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

相关问题 使用杰克逊310 +弹簧启动,得到错误“无法从字符串反序列化类型为java.time.LocalDateTime的值” - Get error “Can not deserialize value of type java.time.LocalDateTime from String” with jackson 310 + Spring boot JSON 解析错误:无法从字符串反序列化“java.time.LocalDateTime”类型的值 - JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 无法从字符串“2020-12-18 16:04:07-06”反序列化“java.time.LocalDateTime”类型的值 - Cannot deserialize value of type `java.time.LocalDateTime` from String “2020-12-18 16:04:07-06” 无法将类型java.lang.String的属性值转换为所需的类型java.time.LocalDateTime - Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime MongoDB 找不到能够从 [java.lang.String] 类型转换为 [java.time.LocalDateTime] 类型的转换器 - MongoDB No converter found capable of converting from type [java.lang.String] to type [java.time.LocalDateTime] 无法从 String 反序列化 LocalDateTime 类型的值 - Cannot deserialize value of type LocalDateTime from String DynamoDBMapper for java.time.LocalDateTime - DynamoDBMapper for java.time.LocalDateTime 找不到能够从类型 [java.time.LocalDateTime] 转换为类型 [java.util.Date] 的转换器 - No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date] Android java.time.LocalDateTime - Android java.time.LocalDateTime java.time.LocalDateTime,多条时间线? - java.time.LocalDateTime, multiple time lines?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM