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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43346906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:21:31  来源:igfitidea点击:

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

javajsonspringdatespring-boot

提问by gstackoverflow

I have following configuration:

我有以下配置:

    @Bean
    @Primary
    public ObjectMapper objectMapper(Hymanson2ObjectMapperBuilder 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.Hymanson.datatype:Hymanson-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.Hymanson.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"])

回答by Jure Kolenko

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::parseBestto parse the date.

如果您实际上并不关心您的日期是什么类型(LocalDate、OffsetDate、ZonedDate...),您可以将其设为TemporalAccessor,然后使用DateTimeFormatter::parseBest来解析日期。

P.S.
string "2014-01-01T00:00:00" will be valid for LocalDateTime

PS
字符串“2014-01-01T00:00:00”对 LocalDateTime 有效