springboot项目中统一时间格式处理方法
作者:飞翔的小->子>弹->
JacksonConfiguration主要用于配置JSON的序列化和反序列化,而LocalDateTimeFormatter则用于处理请求和响应中的LocalDateTime格式,这两个配置项在SpringBoot项目中至关重要,确保数据格式的正确处理和传输
JacksonConfiguration
:
主要用于配置 Jackson 的序列化和反序列化,影响 JSON 请求和响应的内容。适用于处理 JSON 数据的序列化和反序列化
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.deser.std.StringDeserializer; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; 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 java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Configuration public class JacksonConfiguration { @Value("${spring.jackson.date-format}") private String dateTimeFormat; @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { builder.deserializerByType(Long.TYPE, StringDeserializer.instance); builder.serializerByType(Long.class, ToStringSerializer.instance); builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat))); builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat))); SimpleModule module = new SimpleModule(); module.addKeySerializer(LocalDateTime.class, new JsonSerializer<>() { final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(dateTimeFormat); @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeFieldName(FORMATTER.format(value)); } }); builder.modules(module); }; } }
LocalDateTimeFormatter
:
主要用于处理 Spring MVC 中的请求参数和响应体中的 LocalDateTime
。它直接影响 Controller 方法中参数的解析和返回值的格式化。
import org.springframework.context.annotation.Configuration; import org.springframework.format.Formatter; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; @SuppressWarnings("NullableProblems") @Configuration public class LocalDateTimeFormatter implements Formatter<LocalDateTime> { public static final String DATA_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; public static final String DATA_TIME_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss.SSS"; @Override public LocalDateTime parse(String text, Locale locale) { return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(DATA_TIME_FORMAT)); } @Override public String print(LocalDateTime object, Locale locale) { return DateTimeFormatter.ofPattern(DATA_TIME_FORMAT).format(object); } }
到此这篇关于springboot项目中统一时间格式处理的文章就介绍到这了,更多相关springboot统一时间格式处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!