栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Web Jackson日期处理 - 最终篇

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Web Jackson日期处理 - 最终篇

之前介绍过在Spring Web中如何处理Java日期格式,可参见我之前的文章:
《SpringBoot Web Java8日期处理》
今天给出一个最终的解决方案,通过一个配置类可以同时设置Web接收参数、输出结果中的日期格式,
同时支持java.util.Date和Java8+ java.time.*。

配置定义

首先定义配置属性类:

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = WebJacksonProps.PREFIX)
public class WebJacksonProps {
    
    public static final String PREFIX = "web.jackson";
    
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_LOCAL_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_ZONE = "GMT+8";

    
    private Boolean enabled = true;
    
    private String dateFormat = DEFAULT_DATE_FORMAT;
    
    private String localDateFormat = DEFAULT_LOCAL_DATE_FORMAT;
    
    private String localDateTimeFormat = DEFAULT_LOCAL_DATE_TIME_FORMAT;
    
    private String timeZone = DEFAULT_TIME_ZONE;

    //省略getter/setter
}

然后定义配置类:

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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;


@Configuration
@EnableConfigurationProperties({WebJacksonProps.class})
@ConditionalOnProperty(prefix = WebJacksonProps.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class WebJacksonConfiguration implements WebMvcConfigurer {

    
    private WebJacksonProps webJacksonProps;

    public WebJacksonConfiguration(WebJacksonProps webJacksonProps) {
        this.webJacksonProps = webJacksonProps;
    }

    
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            //java.util.Date日期格式
            builder.simpleDateFormat(this.webJacksonProps.getDateFormat());

            //Java8+ java.time.*日期格式
            DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern(this.webJacksonProps.getLocalDateFormat())
                    .withZone(ZoneId.of(this.webJacksonProps.getTimeZone()));
            DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern(this.webJacksonProps.getLocalDateTimeFormat())
                    .withZone(ZoneId.of(this.webJacksonProps.getTimeZone()));
            builder.serializers(new LocalDateSerializer(localDateFormatter));
            builder.serializers(new LocalDateTimeSerializer(localDateTimeFormatter));
            builder.deserializers(new LocalDateDeserializer(localDateFormatter));
            builder.deserializers(new LocalDateTimeDeserializer(localDateTimeFormatter));
        };
    }

    
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //java.util.Date日期格式
        DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
        DateFormatter dateFormatter = new DateFormatter(this.webJacksonProps.getDateFormat());
        dateFormatter.setTimeZone(TimeZone.getTimeZone(this.webJacksonProps.getTimeZone()));
        dateRegistrar.setFormatter(dateFormatter);
        dateRegistrar.registerFormatters(registry);

        //Java8+ java.time.*日期格式
        DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
        dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern(this.webJacksonProps.getLocalDateFormat())
                .withZone(ZoneId.of(this.webJacksonProps.getTimeZone())));
        dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(this.webJacksonProps.getLocalDateTimeFormat())
                .withZone(ZoneId.of(this.webJacksonProps.getTimeZone())));
        dateTimeRegistrar.registerFormatters(registry);
    }
}
使用方式

在Spring Boot Web环境中保证如上配置类WebJacksonConfiguration被扫描加载即可,
默认日期格式如下:

Java类型默认格式示例值
java.util.Dateyyyy-MM-dd HH:mm:ss2022-05-21 10:00:18
java.time.LocalDateyyyy-MM-dd2022-05-21
java.time.LocalDateTimeyyyy-MM-dd HH:mm:ss2022-05-21 10:00:18

亦可通过如下配置修改默认日期格式:

web:
  jackson:
    # Date格式
    date-format: yyyy-MM-dd HH:mm:ss
    # LocalDate格式
    local-date-format: yyyy-MM-dd
    # LocalDateTime格式
    local-date-time-format: yyyy-MM-dd HH:mm:ss
    # 时区
    time-zone: GMT+8

同时:

  • 请求体@RequestBody、响应体@ResponseBody 可通过@JsonFormat(pattern=“yyyy-MM-dd”)注解覆盖默认日期格式
  • 请求参数@RequestParam、请求参数对象可通过@DateTimeFormat(pattern=“yyyy-MM-dd”)注解覆盖默认日期格式

参考:
https://www.baeldung.com/spring-date-parameters
https://www.baeldung.com/spring-boot-formatting-json-dates

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号