|
|
@@ -0,0 +1,173 @@
|
|
|
+package com.kym.admin.config;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
+import com.fasterxml.jackson.core.JsonParser;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationContext;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
|
+import com.fasterxml.jackson.databind.JsonSerializer;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
+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.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.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.convert.converter.Converter;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class DateConfig {
|
|
|
+ private Logger logger = LoggerFactory.getLogger(DateConfig.class);
|
|
|
+
|
|
|
+ public static final String[] DATE_PATTERNS = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd", "HH:mm:ss"};
|
|
|
+ /**
|
|
|
+ * 默认日期时间格式
|
|
|
+ */
|
|
|
+ public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
|
|
+ /**
|
|
|
+ * 默认日期格式
|
|
|
+ */
|
|
|
+ public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
|
|
+ /**
|
|
|
+ * 默认时间格式
|
|
|
+ */
|
|
|
+ public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
|
|
|
+
|
|
|
+
|
|
|
+ private Date formatDateStr(String dateStr) {
|
|
|
+ for (String pattern : DATE_PATTERNS) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(pattern);
|
|
|
+ format.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
|
|
|
+ try {
|
|
|
+ return format.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ //
|
|
|
+ logger.error("format input date error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * LocalDate转换器,用于转换RequestParam和PathVariable参数
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Converter<String, LocalDate> localDateConverter() {
|
|
|
+ return new Converter<>() {
|
|
|
+ @Override
|
|
|
+ public LocalDate convert(String source) {
|
|
|
+ return DateUtil.toLocalDateTime(formatDateStr(source)).toLocalDate();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * LocalDateTime转换器,用于转换RequestParam和PathVariable参数
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Converter<String, LocalDateTime> localDateTimeConverter() {
|
|
|
+ return new Converter<>() {
|
|
|
+ @Override
|
|
|
+ public LocalDateTime convert(String source) {
|
|
|
+ return DateUtil.toLocalDateTime(formatDateStr(source));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * LocalTime转换器,用于转换RequestParam和PathVariable参数
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Converter<String, LocalTime> localTimeConverter() {
|
|
|
+ return new Converter<>() {
|
|
|
+ @Override
|
|
|
+ public LocalTime convert(String source) {
|
|
|
+ return DateUtil.toLocalDateTime(formatDateStr(source)).toLocalTime();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Date转换器,用于转换RequestParam和PathVariable参数
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public Converter<String, Date> dateConverter() {
|
|
|
+ return new Converter<>() {
|
|
|
+ @Override
|
|
|
+ public Date convert(String source) {
|
|
|
+ return formatDateStr(source);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Json序列化和反序列化转换器,用于转换Post请求体中的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)));
|
|
|
+ javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
|
|
|
+ javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
|
|
|
+ javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
|
|
|
+
|
|
|
+
|
|
|
+ //Date序列化和反序列化
|
|
|
+ javaTimeModule.addSerializer(Date.class, new JsonSerializer<>() {
|
|
|
+ @Override
|
|
|
+ public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
|
|
|
+ String formattedDate = formatter.format(date);
|
|
|
+ jsonGenerator.writeString(formattedDate);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ javaTimeModule.addDeserializer(Date.class, new JsonDeserializer<>() {
|
|
|
+ @Override
|
|
|
+ public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
|
|
|
+ String date = jsonParser.getText();
|
|
|
+ try {
|
|
|
+ return format.parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ objectMapper.registerModule(javaTimeModule);
|
|
|
+ return objectMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|