Springboot2 jackson2 LocalDateTime LocalDate DateTime date formatted
Reason: Use Mybatis plus code generator producing a reverse code generated as the LocalDateTime default date format, there are three entities inside the LocalDateTime field, wherein the database has two data fields, a date field does not initialized using @JsonFormat (pattern = "yyyy-mM-dd HH: mm: ss", timezone = "GMT + 08") field data date format is not initialized become yyyy-mM-ddTHH: mm: ss
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
java.util.TimeZone import;
/**
* Jackson2 date serialization and deserialization process
*
* @Author zhaoyang10
* @create 2018/11/27
*/
@Configuration
public class DateConverterConfig {
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
/**
* Jackson2 json null field of the output sequence of the empty string
*
* @return
* @Author zhaoyang10
*/
@Bean
@Primary
//@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule ();
// serialized date format
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer());
// deserialize date format
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer());
objectMapper.registerModule (javaTimeModule);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setTimeZone (TimeZone.getTimeZone ( "Asia / Shanghai"));
return objectMapper;
}
/**
* Date serialization
*/
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(DATETIME_FORMATTER));
}
}
/**
* Date deserialized
*/
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDateTime.parse(p.getValueAsString(), DATETIME_FORMATTER);
}
}
/**
* Date serialization
*/
public class LocalDateSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(DATE_FORMATTER));
}
}
/**
* Date deserialized
*/
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDate.parse(p.getValueAsString(), DATE_FORMATTER);
}
}
/**
* Date serialization
*/
public class LocalTimeSerializer extends JsonSerializer<LocalTime> {
@Override
public void serialize(LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(TIME_FORMATTER));
}
}
/**
* Date deserialized
*/
public class LocalTimeDeserializer extends JsonDeserializer<LocalTime> {
@Override
public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalTime.parse(p.getValueAsString(), TIME_FORMATTER);
}
}
/**
* Receiving the front end of the conversion process parameter date
*
* @return
*/
@Bean
public Converter<String, LocalDateTime> LocalDateTimeConvert() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(@NonNull String source) {
LocalDateTime date = null;
if (StringUtils.isNotBlank(source)) {
date = LocalDateTime.parse(source, DATETIME_FORMATTER);
}
return date;
}
};
}
@Bean
public Converter<String, LocalDate> LocalDateConvert() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(@NonNull String source) {
LocalDate date = null;
if (StringUtils.isNotBlank(source)) {
date = LocalDate.parse(source, DATE_FORMATTER);
}
return date;
}
};
}
@Bean
public Converter<String, LocalTime> LocalTimeConvert() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(@NonNull String source) {
LocalTime time = null;
if (StringUtils.isNotBlank(source)) {
time = LocalTime.parse(source, TIME_FORMATTER);
}
return time;
}
};
}
}