LocalDateTime的json格式化问题

深碍√TFBOYSˉ_ 2023-10-14 01:57 99阅读 0赞

目录

解决:

1、注册日期序列化器

2、自定义LocalDateTime的JSON格式

3、使用第三方库

总结:


实体类中定义了LocalDateTime类型的属性,获取数据会出现以下日期格式问题:

78a87f85ee51450795ed470904e13c6b.png

讲述:

对于LocalDateTime的JSON序列化和反序列化,需要注意它的JSON格式化问题。

LocalDateTime默认的JSON格式LocalDateTime默认序列化为ISO8601格式的字符串,如:”2023-07-25T12:00:00”。

这种格式不太人性化,而且不同的JSON库会有格式差异。

#

解决:

1、注册日期序列化器

为确保全局格式一致,可以注册自定义的LocalDateTime序列化器: (配置类)

  1. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  2. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
  3. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
  4. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
  5. import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.core.convert.converter.Converter;
  9. import java.time.LocalDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. @Configuration
  12. public class LocalDateTimeSerializerConfig {
  13. private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  14. private static final String DATE_PATTERN = "yyyy-MM-dd";
  15. /**
  16. * string转localdatetime
  17. */
  18. @Bean
  19. public Converter<String, LocalDateTime> localDateTimeConverter() {
  20. return new Converter<String, LocalDateTime>() {
  21. @Override
  22. public LocalDateTime convert(String source) {
  23. if (source.trim().length() == 0) {
  24. return null;
  25. }
  26. // 先尝试ISO格式: 2019-07-15T16:00:00
  27. try {
  28. return LocalDateTime.parse(source);
  29. } catch (Exception e) {
  30. return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));
  31. }
  32. }
  33. };
  34. }
  35. /**
  36. * 统一配置
  37. */
  38. @Bean
  39. public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
  40. JavaTimeModule module = new JavaTimeModule();
  41. LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  42. module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
  43. return builder -> {
  44. builder.simpleDateFormat(DATE_TIME_PATTERN);
  45. builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_PATTERN)));
  46. builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
  47. builder.modules(module);
  48. };
  49. }
  50. }

此时再去获取数据,格式就好了。

8c5c0d0958494ae78c8366bcd2145a5b.png

上面的方案全局生效,当全局的格式化方式无法满足我们需求时,我们对日期格式要做特殊的处理:在类的属性上添加注解。

2、自定义LocalDateTime的JSON格式

可以通过@JsonFormat注解自定义格式:

注意:这种方式只针对单个的数据类型,而非全局,每一个都需要注解去格式化日期。(比较麻烦)

  1. @JsonFormat(pattern = "yyyy-MM-dd")
  2. private LocalDateTime createTime;

常用的格式化pattern包括:

- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd
- HH:mm:ss

3、使用第三方库

可以使用像jackson-datatype-jsr310这样的库,提供了自动格式化的LocalDateTime序列化器。

总结:

通过添加@JsonFormat注解或者自定义序列化器,可以解决LocalDateTime的JSON序列化问题,避免格式不统一的问题。

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读