依赖配置

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.datatype</groupId>
  3. <artifactId>jackson-datatype-jsr310</artifactId>
  4. <version>2.9.8</version>
  5. </dependency>

反序列化全局配置

  1. import com.fasterxml.jackson.core.JsonGenerator;
  2. import com.fasterxml.jackson.databind.*;
  3. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  4. import java.io.IOException;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.LocalTime;
  8. import java.time.ZoneOffset;
  9. import java.time.format.DateTimeFormatter;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.core.convert.converter.Converter;
  13. import org.springframework.util.StringUtils;
  14. /**
  15. * Converter 不可优化使用Lambda表达式,否则会出现启动失败的问题
  16. */
  17. @Configuration
  18. public class LocalDateTimeSerializerConfig {
  19. /**
  20. * String --> LocalDate
  21. */
  22. @Bean
  23. public Converter<String, LocalDate> localDateConverter() {
  24. return new Converter<String, LocalDate>() {
  25. @Override
  26. public LocalDate convert(String source) {
  27. if (StringUtils.hasText(source)) {
  28. return LocalDate.parse(source, DateTimeFormatter.ISO_OFFSET_DATE);
  29. }
  30. return null;
  31. }
  32. };
  33. }
  34. /**
  35. * String --> LocalDatetime
  36. */
  37. @Bean
  38. public Converter<String, LocalDateTime> localDateTimeConverter() {
  39. return new Converter<String, LocalDateTime>() {
  40. @Override
  41. public LocalDateTime convert(String source) {
  42. if (StringUtils.hasText(source)) {
  43. return LocalDateTime.parse(source,
  44. DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  45. }
  46. return null;
  47. }
  48. };
  49. }
  50. /**
  51. * String --> LocalTime
  52. */
  53. @Bean
  54. public Converter<String, LocalTime> localTimeConverter() {
  55. return new Converter<String, LocalTime>() {
  56. @Override
  57. public LocalTime convert(String source) {
  58. if (StringUtils.hasText(source)) {
  59. return LocalTime.parse(source, DateTimeFormatter.ISO_OFFSET_TIME);
  60. }
  61. return null;
  62. }
  63. };
  64. }
  65. /**
  66. * Json序列化和反序列化转换器,
  67. 用于转换Post请求体中的json以及将我们的对象序列化为返回响应的json
  68. */
  69. @Bean
  70. public ObjectMapper objectMapper() {
  71. ObjectMapper objectMapper = new ObjectMapper();
  72. objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  73. objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
  74. // LocalDateTime系列序列化模块,继承自jsr310,我们在这里修改了日期格式
  75. JavaTimeModule javaTimeModule = new JavaTimeModule();
  76. javaTimeModule.addSerializer(
  77. LocalDateTime.class,
  78. new JsonSerializer<LocalDateTime>() {
  79. @Override
  80. public void serialize(
  81. LocalDateTime value, JsonGenerator gen,
  82. SerializerProvider serializers)
  83. throws IOException {
  84. String format =
  85. value.atZone(ZoneOffset.UTC)
  86. .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  87. gen.writeString(format);
  88. }
  89. });
  90. javaTimeModule.addSerializer(
  91. LocalDate.class,
  92. new JsonSerializer<LocalDate>() {
  93. @Override
  94. public void serialize(LocalDate value, JsonGenerator gen,
  95. SerializerProvider serializers)
  96. throws IOException {
  97. String format = value.format(DateTimeFormatter.ISO_OFFSET_DATE);
  98. gen.writeString(format);
  99. }
  100. });
  101. objectMapper.registerModule(javaTimeModule);
  102. return objectMapper;
  103. }
  104. }

注意 Converter 实现的时候 不要简写为Lambda表达式,会报错,这是因为丢失类型信息导致的转换失败。