All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.anonymousmister.bootfastconfig.jackson.JacksonConfig Maven / Gradle / Ivy

package com.github.anonymousmister.bootfastconfig.jackson;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static com.github.anonymousmister.bootfastconfig.constant.BeanNameConstant.*;


/**
 * @author mister
 */

public class JacksonConfig {


    @Bean
    public ObjectMapper initObjectMapper(@Qualifier(ANNOTATION_JACKSON) Module module) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(module);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }

    @Bean(MY_HTTP_MESSAGE_CONVERTER)
    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //设置中文编码格式
        List list = new ArrayList();
        list.add(MediaType.APPLICATION_JSON_UTF8);
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
        return mappingJackson2HttpMessageConverter;
    }

    @Bean(JSON_PARAM_ARGUMENT_RESOLVER)
    public HandlerMethodArgumentResolver fastHandlerMethodArgumentResolver(ObjectMapper objectMapper) {
        return new JacksonJsonParam(objectMapper);
    }

    @Configuration
    @ConditionalOnClass(value = {RedisSerializer.class})
    public class RedisConfig {
        @Bean(MY_REDIS_SERIALIZER)
        public RedisSerializer redisSerializerFast(@Qualifier(ANNOTATION_JACKSON) Module module) {
            //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
            Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            mapper.registerModule(module);
            serializer.setObjectMapper(mapper);
            return serializer;
        }

    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy