当前位置:   article > 正文

SpringBoot解决LocalDateTime_cannot deserialize value of type `java.time.locald

cannot deserialize value of type `java.time.localdatetime` from string "2024

项目场景:开发问题记录

例如: 前端传入时间报错,无法反序列化。向前端返回时间需要格式化


问题描述

前端传入的时间无法反序列化,向前端返回格式化时间

  1. Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-04-06 14:33:33": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-04-06 14:33:33' could not be parsed at index 10
  2.  at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 4, column: 18] (through reference chain: com.taiji.businessdomain.stmbsscpacdc.client.api.dto.persist.PersistExpressInfoRequest["updatedAt"])

 Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-04-06 14:33:33": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-04-06 14:33:33' could not be parsed at index 10
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 4, column: 18] (through reference chain: com.taiji.businessdomain.stmbsscpacdc.client.api.dto.persist.PersistExpressInfoRequest["updatedAt"])


原因分析:

提示:这里填写问题的分析:

时间格式导致无法反序列化,前端时间默认格式一般为“2023-04-06T14:33:14.997Z”,而"2023-04-06 14:33:33"这种时间格式化无法接收


解决方案1:

提示:这里填写该问题的具体解决方案:

直接在属性上加上注解

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")

 

解决方案2:

提示:这里填写该问题的具体解决方案:

通过jackson来自定义一个全局类型处理,通过序列化可以将返回前端的时间进行格式化,反序列化将前端传来的时间进行格式化

  1. package com.taiji.config;
  2. import com.fasterxml.jackson.core.JsonParser;
  3. import com.fasterxml.jackson.databind.DeserializationContext;
  4. import com.fasterxml.jackson.databind.JsonDeserializer;
  5. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
  6. import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import java.io.IOException;
  10. import java.time.Instant;
  11. import java.time.LocalDateTime;
  12. import java.time.ZoneId;
  13. import java.time.format.DateTimeFormatter;
  14. @Configuration
  15. public class LocalDateTimeSerializerConfig {
  16. @Bean
  17. public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
  18. return builder -> {
  19. //序列化
  20. builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  21. //反序列化
  22. builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
  23. };
  24. }
  25. /**
  26. * 反序列化
  27. */
  28. public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
  29. @Override
  30. public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
  31. throws IOException {
  32. long timestamp = p.getValueAsLong();
  33. if (timestamp > 0) {
  34. return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
  35. } else {
  36. return null;
  37. }
  38. }
  39. }
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/692544
推荐阅读
相关标签
  

闽ICP备14008679号