Java8中对于LocalDateTime的序列化和反序列化问题
作者:刘元涛
这篇文章主要介绍了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 即可。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
