springboot统一时间格式处理

之前我们做过关于前台日期格式统一处理,其实也就是把要显示的时间进行了处理,增强可读性。

但是增删改查功能中,LocalDateTime系列作为dto中时间的类型,

经常会报类似这样的错误:

Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty java.time.LocalDateTime] for value '2021-03-16T00:00:00'; 

想解决这样的问题可以给实体类加入@DateTimeFormat(用于前台传递到后台)和@JsonFormat(后台传递到前台),但是这样就得给每个时间相关的属性,显然是治标不治本,也过于繁琐。

如果要彻底处理这个问题,就需要后台做好统一时间格式处理,根据类型做适当转换。

前台传递到后台时,将json中的String类型转化成后台需要的时间格式

后台传递到前台显示时,序列化过程中做好时间类型切换

这里以LocalDate/LocalDateTime为例,代码如下:

package com.one.market.config;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

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.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

@Configuration
public class DateConfig {

	/** 默认日期时间格式 */
	public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	/** 默认日期格式 */
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	/** 默认时间格式 */
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

	/**
	 * 前端到后台 LocalDate转换器,用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalDate> localDateConverter() {
		return new Converter<>() {
			@Override
			public LocalDate convert(String source) {
				if (StringUtils.isEmpty(source)) {
					return null;
				}
				return LocalDate.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));

			}
		};
	}

	/**
	 * 前端到后台 LocalDateTime转换器,用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalDateTime> localDateTimeConverter() {
		return new Converter<>() {
			@Override
			public LocalDateTime convert(String source) {
				if (StringUtils.isEmpty(source)) {
					return null;
				}
				return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
			}
		};
	}

	/**
	 * 前端到后台 LocalTime转换器,用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalTime> localTimeConverter() {
		return new Converter<>() {
			@Override
			public LocalTime convert(String source) {
				if (StringUtils.isEmpty(source)) {
					return null;
				}
				return LocalTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
			}
		};
	}

	/**
	 * 后台到前端 Json序列化转换器,用于对象序列化为返回响应的json
	 */
	@Bean
	public ObjectMapper objectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

		// LocalDateTime系列序列化,继承自jsr310,我们在这里修改了日期格式
		JavaTimeModule javaTimeModule = new JavaTimeModule();
		javaTimeModule.addSerializer(LocalDateTime.class,
				new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
		javaTimeModule.addSerializer(LocalDate.class,
				new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
		javaTimeModule.addSerializer(LocalTime.class,
				new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));

		objectMapper.registerModule(javaTimeModule);
		return objectMapper;
	}

}

这样,vue前台就不需要再做处理,数据传递过来时就可以直接显示!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟茜

随多随少随你心意^-^

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值