欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8中對(duì)于LocalDateTime的序列化和反序列化問(wèn)題

 更新時(shí)間:2023年06月01日 14:52:51   作者:劉元濤  
這篇文章主要介紹了Java8中對(duì)于LocalDateTime的序列化和反序列化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java8對(duì)于LocalDateTime的序列化和反序列化

這里以jackjson為例

配置反序列化工具

/**
 * 時(shí)間戳反序列化時(shí)間
 *
 * @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);
? ? ? ? ? ? //返回時(shí)間數(shù)據(jù)序列化
? ? ? ? ? ? builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
? ? ? ? ? ? //接收時(shí)間數(shù)據(jù)反序列化
? ? ? ? ? ? builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
? ? ? ? };
? ? }
}

解決2:

在LocalDateTime 實(shí)體類使用注解

@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時(shí)間類序列化和反序列化的支持

由于項(xiàng)目將原有的  Date類型的字段改造為 LocalDate,LocalDateTime,LocalTime 類型, 發(fā)現(xiàn)  spring  對(duì)項(xiàng)目的時(shí)間格式無(wú)法自動(dòng)轉(zhuǎn)換,故需手動(dòng)配置下。

在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>

因?yàn)? 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 {
? ? /** 默認(rèn)日期時(shí)間格式 */
? ? public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
? ? /** 默認(rèn)日期格式 */
? ? public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
? ? /** 默認(rèn)時(shí)間格式 */
? ? public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
}

然后只需要在實(shí)體類中對(duì)應(yīng)的時(shí)間類型上使用  @DateTimeFormat  和  @JsonFormat  即可。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論