Java8中对于LocalDateTime的序列化和反序列化问题
Java8对于LocalDateTime的序列化和反序列化
这里以jackjson为例
配置反序列化工具
/**
* 时间戳反序列化时间
*
* @author liuyuantao
*/
public class Str2LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
private static final String STANDARD_PATTERN = "yyyy-MM-dd HH:mm:ss";
@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern(STANDARD_PATTERN);
String timeStr = jsonParser.getValueAsString();
return LocalDateTime.parse(timeStr, formatterDateTime);
}
}解决1:
配置全局日期格式化
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
//返回时间数据序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
//接收时间数据反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
};
}
}解决2:
在LocalDateTime 实体类使用注解
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime issueDate;
springboot添加LocalDateTime等java8时间类序列化和反序列化的支持
由于项目将原有的 Date类型的字段改造为 LocalDate,LocalDateTime,LocalTime 类型, 发现 spring 对项目的时间格式无法自动转换,故需手动配置下。
在spring boot 中需在 maven 中引入 jsr-310 的支持
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
或者直接引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency>
因为 spring boot 是使用 jackson 作为 json 序列化和反序列化工具的,故只需配置 jackson 即可。
配置如下:
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_DATE_FORMAT)));
javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_TIME_FORMAT)));
objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
return objectMapper;
}
}public class Constants {
/** 默认日期时间格式 */
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** 默认日期格式 */
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
/** 默认时间格式 */
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
}然后只需要在实体类中对应的时间类型上使用 @DateTimeFormat 和 @JsonFormat 即可。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot快速集成jxls-poi(自定义模板,支持本地文件导出,在线文件导出)
这篇文章主要介绍了SpringBoot快速集成jxls-poi(自定义模板,支持本地文件导出,在线文件导出),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09
Spring Security实现禁止用户重复登陆的配置原理
这篇文章主要介绍了Spring Security实现禁止用户重复登陆的配置原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-12-12
Java中comparator接口和Comparable接口的比较解析
这篇文章主要介绍了Java中comparator接口和Comparable接口的比较解析,Java提供了一个用于比较的接口Comparator和Comparable接口,提供了一个比较的方法,所有实现该接口的类,都动态的实现了该比较方法,需要的朋友可以参考下2023-08-08
Spring中的拦截器HandlerInterceptor详细解析
这篇文章主要介绍了Spring中的拦截器HandlerInterceptor详细解析,HandlerInterceptor 是 Spring 框架提供的一个拦截器接口,用于在请求处理过程中拦截和处理请求,需要的朋友可以参考下2024-01-01


最新评论