SpringBoot日期格式化问题

然而,在Spring Boot进行Restful接口开发中使用这些日期时间类型时,你会发现使用jackson的spring.jackson.date-format配置进行日期类型格式化是无效的,为什么呢?

//以下是配置了`spring.jackson.date-format=yyyy-MM-dd HH:mm:ss`后,
//接口返回LocalDateTime和Date的内容,你会发现Date类型的已经格式化,
//但是LocalDateTime却没有。
{
    "localDateTime": "2019-07-14T12:20:23.615",
    "date": "2019-07-14 04:20:23"
}

 

实际上在Jackson序列化的时候,会根据不同类型调用不同的Serializer进行序列化,如果没有默认的Serializer时,调用的是类的toString()。而Date类型序列化时会使用DateSerializer,里面会使用spring.jackson.date-format中的配置,而LocalDateTime则是通过LocalDateTimeSerializerLocalDateTimeSerializer默认使用的是DateTimeFormatter.ISO_LOCAL_DATE_TIME,所以就会返回上面的结果。


如何处理?

 

通过上文可以知道,如果能够覆盖默认的LocalDateTimeSerializer,就可以按照自己的需求进行日期格式化了。

通过以下方式可以实现:

  • 自定义Jackson2ObjectMapperBuilderCustomizer的Bean,分别指定LocalDate、LocalDateTime和LocalTime的序列化和反序列化类(按需要定义对应的DatetimeFormatter),参考代码如下:
  •  
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;

/**
 * @Author Cent.Chen  2019-07-14
 * @Description Dateformat Configuration.
 */
@Configuration
public class DateformatConfig {

    /**
     * Date格式化字符串
     */
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * DateTime格式化字符串
     */
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * Time格式化字符串
     */
    private static final String TIME_FORMAT = "HH:mm:ss";

    /**
     * 自定义Bean
     *
     * @return
     */
    @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)));
    }

}
  • 重启服务再次测试,返回格式化日期如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值