赞
踩
入参解析Java8时间类型失败
Caused by: java.time.format.DateTimeParseException: Text '1991-04-05 18:10:51' could not be parsed at index 10
第一种方案,覆盖了ObjectMapper,改了好几版了,
- package com.ruoyi.talent.config;
-
- import com.fasterxml.jackson.annotation.JsonInclude;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.databind.*;
- import com.fasterxml.jackson.databind.module.SimpleModule;
- import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
- import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
- import org.springframework.stereotype.Component;
-
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
-
- @Configuration
- public class JsonConvertConfig {
-
- @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
- private String pattern;
-
- private String pattern2 = "yyyy-MM-dd";
- public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
- @Bean
- @Primary
- public ObjectMapper serializingObjectMapper() throws InterruptedException {
- // 3种时间类型转换处理
- ObjectMapper objectMapper = new ObjectMapper();
- JavaTimeModule javaTimeModule = new JavaTimeModule();
- javaTimeModule.addSerializer(Date.class, new DateSerializer());
- javaTimeModule.addDeserializer(Date.class, new DateDeserializer());
- javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
- javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
- javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
- javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
- objectMapper.registerModule(javaTimeModule);
-
- // Long类型转换处理 解决springBoot long类型 长id 到前端丢失精度问题
- // module.addSerializer(Long.class, ToStringSerializer.instance);
- // module.addSerializer(Long.TYPE, ToStringSerializer.instance);
- // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- SimpleModule idLongModule = new SimpleModule();
- idLongModule.addSerializer(Long.class, new IdLongSerializer());
- idLongModule.addSerializer(Long.TYPE, new IdLongSerializer());
- objectMapper.registerModule(idLongModule);
- // 添加此配置解决,Springboot 多传参数导致 JSON parse error: Unrecognized filed ...异常
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- return objectMapper;
- }
- // 默认序列化成数值,超过一定范围再转换为字符串类型
- public class IdLongSerializer extends JsonSerializer<Long> {
- @Override
- public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
- if (value > 900000000000000L) {
- gen.writeString(value.toString());
- } else {
- gen.writeNumber(value);
- }
- }
- }
-
- /**
- * @author xiaofu
- * @description Date 时间类型装换
- * @date 2020/9/1 17:25
- */
- @Component
- public class DateSerializer extends JsonSerializer<Date> {
- @Override
- public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
- String formattedDate = dateFormat.format(date);
- gen.writeString(formattedDate);
- }
- }
-
- /**
- * @author xiaofu
- * @description Date 时间类型装换
- * @date 2020/9/1 17:25
- */
- @Component
- public class DateDeserializer extends JsonDeserializer<Date> {
-
- @Override
- public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
- try {
- return dateFormat.parse(jsonParser.getValueAsString());
- } catch (ParseException e) {
- throw new RuntimeException("Could not parse date", e);
- }
- }
- }
-
- /**
- * @author xiaofu
- * @description LocalDate 时间类型装换
- * @date 2020/9/1 17:25
- */
- public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
- @Override
- public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
- gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
- }
- }
-
- /**
- * @author xiaofu
- * @description LocalDate 时间类型装换
- * @date 2020/9/1 17:25
- */
- public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
- @Override
- public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
- return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));
- }
- }
-
- /**
- * @author xiaofu
- * @description LocalDate 时间类型装换
- * @date 2020/9/1 17:25
- */
- public class LocalDateSerializer extends JsonSerializer<LocalDate> {
- @Override
- public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
- gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern2)));
- }
- }
-
- /**
- * @author xiaofu
- * @description LocalDate 时间类型装换
- * @date 2020/9/1 17:25
- */
- public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
- @Override
- public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
- return LocalDate.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern2));
- }
- }
- }
第二种方案,不覆盖,但是有坑,实际尝试后会导致静态资源无法访问
一文详解JackSon配置信息 - 知乎
文章来源:
https://juejin.cn/post/6867722037796798478
3种 Springboot 全局时间格式化方式,别再写重复代码了-CSDN博客w
https://www.cnblogs.com/guanxiaohe/p/17684403.html
一文详解JackSon配置信息 - 知乎
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。