Jackson
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.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* @author Qbit
*/
@Slf4j
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
log.info("use "+Jackson2ObjectMapperBuilderCustomizer.class.getSimpleName()+" from "+JacksonConfig.class.getName());
return jacksonObjectMapperBuilder -> {
jacksonObjectMapperBuilder.serializerByType(LocalDateTime.class, LocalDateTimeSerializer.getInstance());
jacksonObjectMapperBuilder.deserializerByType(LocalDateTime.class, LocalDateTimeDeserializer.getInstance());
jacksonObjectMapperBuilder.deserializerByType(LocalTime.class,new LocalDateTimeDeserializer());
};
}
/**
* @author Qbit
*/
public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
private static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer();
public static LocalDateTimeDeserializer getInstance() {
return INSTANCE;
}
@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
return LocalDateTime.parse(jsonParser.getValueAsString(),DateTimeFormatters.yyyy_MM_dd_HH_mm_ss);
}
}
public static class LocalTimeDeserializer extends JsonDeserializer<LocalTime> {
@Override
public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
return Transformers.toLocalTime(jsonParser.getValueAsString());
}
}
/**
* @author Qbit
*/
public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
private static final LocalDateTimeSerializer INSTANCE=new LocalDateTimeSerializer();
public static LocalDateTimeSerializer getInstance() {
return INSTANCE;
}
@Override
public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(localDateTime.format(DateTimeFormatters.yyyy_MM_dd_HH_mm_ss));
}
}
}
SpringBoot默认使用的Jackson来做json格式的转换,所以将自己对特定格式的转换类注册进去。
Converter
需要注意的是上面的只针对入参和出参是json格式时生效,如果入参是Paramenter则不会生效,这时需要提供自定的Converter
LocalDateTimeConverter
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String s) {
return Transformers.toLocalDateTime(s);
}
}
工具类
public class Transformers {
public static DateTimeFormatter yyyy_MM_dd_HH_mm_ss=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static DateTimeFormatter HH_mm_ss=DateTimeFormatter.ofPattern("HH:mm:ss");
public static LocalDateTime toLocalDateTime(String s) {
return LocalDateTime.parse(s, yyyy_MM_dd_HH_mm_ss);
}
public static LocalTime toLocalTime(String s) {
return LocalTime.parse(s, HH_mm_ss);
}
}