package com.mzl.flower.config;
|
|
import com.fasterxml.jackson.databind.*;
|
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
|
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.ZonedDateTimeSerializer;
|
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
|
import java.text.SimpleDateFormat;
|
import java.time.*;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
|
@Configuration
|
public class Java8TimeConfig {
|
|
@Value("${spring.jackson.date-format}")
|
private String formatValue;
|
|
@Bean(name = "format")
|
DateTimeFormatter format() {
|
return DateTimeFormatter.ofPattern(formatValue);
|
}
|
|
@Bean
|
public ObjectMapper serializingObjectMapper(@Qualifier("format") DateTimeFormatter format) {
|
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(format));
|
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(format));
|
javaTimeModule.addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
|
ObjectMapper mapper = new ObjectMapper()
|
.registerModule(new ParameterNamesModule())
|
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
.registerModule(javaTimeModule);
|
return mapper;
|
}
|
|
}
|