zoukankan      html  css  js  c++  java
  • SpringBoot JackSon全局配置

    package com.nxe.business.bailian.bi.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.databind.*;
    import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
    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.ser.LocalDateSerializer;
    import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    import com.nex.bu1.json.JacksonEx;
    import com.nex.bu1.util.DateEx;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    
    /**
     * jackson 全局配置
     * @author Yungui.Zheng
     * @date 2020-11-05
     */
    @Configuration
    public class CustomNullValueSerializerConfig {
    
        @Bean
        public ObjectMapper objectMapper() {
            final ObjectMapper mapper = new ObjectMapper();
    
            mapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    
            // Date
            mapper.setDateFormat(new SimpleDateFormat(DateEx.FMT_YMD_HMS));
            // LocalDate
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(
                    LocalDate.class, new LocalDateSerializer(DateEx.formatter(DateEx.FMT_YMD)));
            javaTimeModule.addDeserializer(
                    LocalDate.class, new LocalDateDeserializer(DateEx.formatter(DateEx.FMT_YMD)));
            // LocalDateTime
            javaTimeModule.addSerializer(
                    LocalDateTime.class, new LocalDateTimeSerializer(DateEx.formatter()));
            javaTimeModule.addDeserializer(
                    LocalDateTime.class, new LocalDateTimeDeserializer(DateEx.formatter()));
    
            mapper.registerModule(javaTimeModule);
            mapper.registerModule(new Jdk8Module());
            return mapper;
        }
    
    }
    

      Jackson在spring框架中用来实现Json和对象的相互转化,通过自定义配置实现对应转化需求

  • 相关阅读:
    跨域上传文件
    算法
    websocket
    Bottle
    爬虫一
    RabbitMQ
    git&github快速入门
    centos6安装SaltStack
    ajax
    Django之Model操作
  • 原文地址:https://www.cnblogs.com/irobotzz/p/13932935.html
Copyright © 2011-2022 走看看