springboot2 LocalDateTime类型未生效

项目背景:springboot 2.1.8release

application.properties配置:

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

postgres数据库类型:create_time timestamp

实体类型:

@TableFiled
private LocalDateTime createTime;

查询结果:

'createTime':''2020-04-14 19:58:55.764'

纳尼?不应该是:‘createTime’:‘2020-04-14T19:58:55.764’?为什么?

spring.jackson.date-formate='yyyy-MM-dd HH:mm:ss'为什么没生效呢???

查阅资料得知:

1·、jdk8的LocalDaeTime LocalDate LocalTime默认序列化和反序列化是toString(),而Date timestamp有各自的序列化和反序列化处理类。

2、spring.jackson.date-formate='yyyy-MM-dd HH:mm:ss'只针对于Date timestamp类型有效。

那么怎么解决这个问题呢?重写jackson的关于LocalDateTime LocalTime LocalDate的序列化和反序列化的处理类。

具体的解决方案如下;

package com.skyeye.config;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * format LocalDateTime LocalDate LocalTime
 *
 * @author Jiantao Li
 * @date 2020/4/15 11:04
 */
@Configuration
public class LocalDateFormatConfig {

    /**
     * Date格式化字符串
     */
    private static final String DATE_FORMAT = DatePattern.NORM_DATE_PATTERN;
    /**
     * DateTime格式化字符串
     */
    private static final String DATETIME_FORMAT = DatePattern.NORM_DATETIME_PATTERN;
    /**
     * Time格式化字符串
     */
    private static final String TIME_FORMAT = DatePattern.NORM_TIME_PATTERN;

    @Bean
    @Primary
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
                .serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)))
                .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
                .deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
    }
}

重写LocalDateTime LocalDate LocalTime的序列化和反序列化处理类。

项目中遇到的问题分享与大家。

本文完。

 

你可能感兴趣的:(学习)