当前位置:   article > 正文

LocalDateTime入参反序列化(新增LocalDate处理)和springBoot long类型 长id 到前端丢失精度问题

LocalDateTime入参反序列化(新增LocalDate处理)和springBoot long类型 长id 到前端丢失精度问题

入参解析Java8时间类型失败
Caused by: java.time.format.DateTimeParseException: Text '1991-04-05 18:10:51' could not be parsed at index 10
 

第一种方案,覆盖了ObjectMapper,改了好几版了,

  1. package com.ruoyi.talent.config;
  2. import com.fasterxml.jackson.annotation.JsonInclude;
  3. import com.fasterxml.jackson.core.JsonGenerator;
  4. import com.fasterxml.jackson.core.JsonParser;
  5. import com.fasterxml.jackson.databind.*;
  6. import com.fasterxml.jackson.databind.module.SimpleModule;
  7. import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
  8. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.context.annotation.Primary;
  14. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
  15. import org.springframework.stereotype.Component;
  16. import java.io.IOException;
  17. import java.text.DateFormat;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.time.LocalDate;
  21. import java.time.LocalDateTime;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.Date;
  24. @Configuration
  25. public class JsonConvertConfig {
  26. @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
  27. private String pattern;
  28. private String pattern2 = "yyyy-MM-dd";
  29. public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  30. @Bean
  31. @Primary
  32. public ObjectMapper serializingObjectMapper() throws InterruptedException {
  33. // 3种时间类型转换处理
  34. ObjectMapper objectMapper = new ObjectMapper();
  35. JavaTimeModule javaTimeModule = new JavaTimeModule();
  36. javaTimeModule.addSerializer(Date.class, new DateSerializer());
  37. javaTimeModule.addDeserializer(Date.class, new DateDeserializer());
  38. javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
  39. javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
  40. javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
  41. javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
  42. objectMapper.registerModule(javaTimeModule);
  43. // Long类型转换处理 解决springBoot long类型 长id 到前端丢失精度问题
  44. // module.addSerializer(Long.class, ToStringSerializer.instance);
  45. // module.addSerializer(Long.TYPE, ToStringSerializer.instance);
  46. // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  47. SimpleModule idLongModule = new SimpleModule();
  48. idLongModule.addSerializer(Long.class, new IdLongSerializer());
  49. idLongModule.addSerializer(Long.TYPE, new IdLongSerializer());
  50. objectMapper.registerModule(idLongModule);
  51. // 添加此配置解决,Springboot 多传参数导致 JSON parse error: Unrecognized filed ...异常
  52. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  53. return objectMapper;
  54. }
  55. // 默认序列化成数值,超过一定范围再转换为字符串类型
  56. public class IdLongSerializer extends JsonSerializer<Long> {
  57. @Override
  58. public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  59. if (value > 900000000000000L) {
  60. gen.writeString(value.toString());
  61. } else {
  62. gen.writeNumber(value);
  63. }
  64. }
  65. }
  66. /**
  67. * @author xiaofu
  68. * @description Date 时间类型装换
  69. * @date 2020/9/1 17:25
  70. */
  71. @Component
  72. public class DateSerializer extends JsonSerializer<Date> {
  73. @Override
  74. public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
  75. String formattedDate = dateFormat.format(date);
  76. gen.writeString(formattedDate);
  77. }
  78. }
  79. /**
  80. * @author xiaofu
  81. * @description Date 时间类型装换
  82. * @date 2020/9/1 17:25
  83. */
  84. @Component
  85. public class DateDeserializer extends JsonDeserializer<Date> {
  86. @Override
  87. public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  88. try {
  89. return dateFormat.parse(jsonParser.getValueAsString());
  90. } catch (ParseException e) {
  91. throw new RuntimeException("Could not parse date", e);
  92. }
  93. }
  94. }
  95. /**
  96. * @author xiaofu
  97. * @description LocalDate 时间类型装换
  98. * @date 2020/9/1 17:25
  99. */
  100. public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
  101. @Override
  102. public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  103. gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
  104. }
  105. }
  106. /**
  107. * @author xiaofu
  108. * @description LocalDate 时间类型装换
  109. * @date 2020/9/1 17:25
  110. */
  111. public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
  112. @Override
  113. public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
  114. return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));
  115. }
  116. }
  117. /**
  118. * @author xiaofu
  119. * @description LocalDate 时间类型装换
  120. * @date 2020/9/1 17:25
  121. */
  122. public class LocalDateSerializer extends JsonSerializer<LocalDate> {
  123. @Override
  124. public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  125. gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern2)));
  126. }
  127. }
  128. /**
  129. * @author xiaofu
  130. * @description LocalDate 时间类型装换
  131. * @date 2020/9/1 17:25
  132. */
  133. public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
  134. @Override
  135. public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
  136. return LocalDate.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern2));
  137. }
  138. }
  139. }

第二种方案,不覆盖,但是有坑,实际尝试后会导致静态资源无法访问
一文详解JackSon配置信息 - 知乎


文章来源:

https://juejin.cn/post/6867722037796798478

3种 Springboot 全局时间格式化方式,别再写重复代码了-CSDN博客w
https://www.cnblogs.com/guanxiaohe/p/17684403.html
一文详解JackSon配置信息 - 知乎







 

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

闽ICP备14008679号