SpringBoot中自定义类型转换(以LocalDateTime为例)

tech2023-11-09  97

文章目录

JacksonConverter

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

import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import java.time.LocalTime; @Component public class LocalTimeConverter implements Converter<String, LocalTime> { @Override public LocalTime convert(String source) { return Transformers.toLocalTime(source); } }
最新回复(0)