入参解析Java8时间类型失败
Caused by: java.time.format.DateTimeParseException: Text '1991-04-05 18:10:51' could not be parsed at index 10
第一种方案,覆盖了ObjectMapper,改了几版,
2024年6月19日16:35:09加了时区配置
package com.ruoyi.talent.config;
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.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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;
import java.util.TimeZone;
@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();
// 设置ObjectMapper的时区
objectMapper.setTimeZone(TimeZone.getDefault());
// 其他时间配置
JavaTimeModule javaTimeModule = new JavaTimeModule();
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 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配置信息 - 知乎