无法从String反序列化java.time.LocalDateTime类型的值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了无法从String反序列化java.time.LocalDateTime类型的值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2479字,纯文字阅读大概需要4分钟。
内容图文
![无法从String反序列化java.time.LocalDateTime类型的值](/upload/InfoBanner/zyjiaocheng/735/8936c5cd08334b2db6d4e12b24452a74.jpg)
我有以下配置:
@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;
}
以及依赖关系:
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')
}
我添加了以下控制器:
@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;
如果我像这样传递json:
{
"num1":"324",
"num2":123,
"date":"2014-01-01"
}
应用程序打印输出:
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"])
解决方法:
原始答案:
java中的LocalDateTime不接受“2014-01-01”作为有效日期字符串.
一些额外的信息:
如果您实际上并不关心您的日期类型(LocalDate,OffsetDate,ZonedDate,…),您可以将其设为TemporalAccessor,然后使用DateTimeFormatter::parseBest来解析日期.
附:
字符串“2014-01-01T00:00:00”对LocalDateTime有效
内容总结
以上是互联网集市为您收集整理的无法从String反序列化java.time.LocalDateTime类型的值全部内容,希望文章能够帮你解决无法从String反序列化java.time.LocalDateTime类型的值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。