美文网首页
jackson序列化配置

jackson序列化配置

作者: 无我_无他_有你 | 来源:发表于2021-07-12 14:41 被阅读0次

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author wqf
 * @date 2021/07/11
 * 类描述:
 */
@Configuration
public class JacksonConfig {

    /**
     * 默认日期时间格式
     */
    private static final String LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 默认日期格式
     */
    private static final String LOCAL_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 默认时间格式
     */
    private static final String DATE_TIME_FORMAT = "HH:mm:ss";

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        //时间序列化
        builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //针对于JDK新时间类。序列化时带有T的问题,自定义格式化字符串
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //LocalDateTime
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));
        //LocalDate
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));
        //LocalTime
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
        builder.modules(javaTimeModule);
        //序列化将BigDecimal转String类型 BigDecimal数据返回到前端会出现精度丢失的问题 不建议使用
        builder.featuresToDisable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        //Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
        //builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        //未知字段不序列化
        builder.failOnUnknownProperties(false);
        //不允许出现特殊字符和转义符 默认不允许
        builder.featuresToDisable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
        // 不允许出现单引号 默认不允许
        builder.featuresToDisable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        //默认关闭,将char[]数组序列化为String类型。若开启后序列化为JSON数组。
        builder.featuresToDisable(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS);
        //默认情况下,在对ZonedDateTime进行反序列化期间,Jackson会将解析的时区调整为上下文提供的时区.您可以使用此设置修改此行为,以使解析的ZonedDateTime保持在Z而不是UTC
        builder.featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
        return builder.createXmlMapper(false).build();
    }

}

相关文章

网友评论

      本文标题:jackson序列化配置

      本文链接:https://www.haomeiwen.com/subject/tbjnrltx.html