阅读 154

SpringBoot全局配置日期参数格式化

背景

项目中会经常出现传递日期参数的情形,虽然我们可以通过@DateTimeFormat(pattern = "yyyy-MM-dd")的方式对父类或单独的字段进行设置,但每次都要设置比较繁琐,所以我们可以通过全局配置来进行一次性设置大部分的情况,特例的格式化采用注解的方式。

完成目标

  • 请求入参为 String(指定格式)转 Date,支持getpost
  • 支持Java8 日期 API,如:LocalDateLocalDateTime

定义DateConvertConfig类

package com.stop.think.web.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * @author stopThink
 */
@Configuration
public class DateConvertConfig {

  @Bean
  public Converter<String, LocalDate> localDateConverter() {
    return new Converter<String, LocalDate>() {
      @Override
      public LocalDate convert (String source) {
        return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
      }
    };
  }

  @Bean
  public Converter<String, LocalDateTime> localDateTimeConverter() {
    return new Converter<String, LocalDateTime>() {
      @Override
      public LocalDateTime convert (String source) {
        return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
      }
    };
  }

  /**
   * 更改jackson默认配置
   * 处理post形式,传递json
   */
  @Bean
  public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    // 对于空的对象转json的时候不抛出错误
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    // 禁用遇到未知属性抛出异常
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    // 日期和时间格式化
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    objectMapper.registerModule(javaTimeModule);
    return objectMapper;
  }
}

复制代码

注意: 关于自定义转换器Converter由匿名类转化为lambda表达式的问题,网上大部分的文章说是当使用lambda表达式后,需要在对应的方法上添加@ConditionalOnBean(name = "requestMappingHandlerAdapter")注解,否则会发生异常,但经过我的测试,增加这个注解后,并不能正确进行日期格式化,所测试的SpringBoot版本为2.3.X以上。

文章分类
后端
文章标签