spring boot 잭 슨 반환 null 문제 해결

24860 단어 springboot자바
spring boot 의 기본 jackson 직렬 화 대상 을 사용 합 니 다.필드 가 null 이면 전단 에 우호 적 이지 않 으 므 로 빈 대상 으로 처리 해 야 합 니 다.
사용자 정의 대상 직렬 화 수정 기
public class CustomBeanSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config,BeanDescription beanDesc,List<BeanPropertyWriter> beanProperties) {
        for (int i = 0; i < beanProperties.size(); i++) {
            BeanPropertyWriter writer = beanProperties.get(i);
            if (isArrayType(writer)) {
                writer.assignNullSerializer(new CustomNullJsonSerializers.NullArrayJsonSerializer());
            } else if (isStringType(writer)) {
                writer.assignNullSerializer(new CustomNullJsonSerializers.NullStringJsonSerializer());
            }else if (isNumberType(writer)) {
                writer.assignNullSerializer(new CustomNullJsonSerializers.NullNumberJsonSerializer());
            }else if (isBooleanType(writer)) {
                writer.assignNullSerializer(new CustomNullJsonSerializers.NullBooleanJsonSerializer());
            }else{
                writer.assignNullSerializer(new CustomNullJsonSerializers.NullObjectJsonSerializer());
            }
        }
        return beanProperties;
    }

    /**
     *      
     */
    private boolean isArrayType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
    }

    /**
     *    String
     */
    private boolean isStringType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
    }

    /**
     *        
     */
    private boolean isNumberType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return Number.class.isAssignableFrom(clazz);
    }

    /**
     *    boolean
     */
    private boolean isBooleanType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.equals(Boolean.class);
    }
  • 서로 다른 유형의 빈 대상 의 직렬 화 방식 을 사용자 정의 합 니 다
  • public class CustomNullJsonSerializers {
    
        /**
         *          null 
         */
        public static class NullArrayJsonSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeStartArray();
                jsonGenerator.writeEndArray();
            }
        }
    
        /**
         *         null 
         */
        public static class NullStringJsonSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object value, JsonGenerator jsonGenerator,SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        }
    
        /**
         *        null 
         */
        public static class NullNumberJsonSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object value, JsonGenerator jsonGenerator,SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeNumber(0);
            }
        }
    
        /**
         *   boolean   null 
         */
        public static class NullBooleanJsonSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object value, JsonGenerator jsonGenerator,SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeBoolean(false);
            }
        }
    
        /**
         *          null 
         */
        public static class NullObjectJsonSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object value, JsonGenerator jsonGenerator,SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeNull();
            }
        }
    
    }
    
    
  • Jackson HTTP 변환기 설정
  • public class JacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {
    
        public JacksonHttpMessageConverter(){
            super.getObjectMapper().setSerializerFactory(super.getObjectMapper().getSerializerFactory().withSerializerModifier(new CustomBeanSerializerModifier()));
        }
    }
    

    jsckson ObjectMapper 설정
    @Configuration
    public class JacksonAutoConfigure {
    
        @Bean
        @Primary
        @ConditionalOnMissingBean(ObjectMapper.class)
        public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
            ObjectMapper objectMapper = builder.createXmlMapper(false).build();
            objectMapper.setSerializerFactory(objectMapper.getSerializerFactory().withSerializerModifier(new CustomBeanSerializerModifier()));
            SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
            serializerProvider.setNullValueSerializer(new CustomNullJsonSerializers.NullObjectJsonSerializer());
            return objectMapper;
        }
    }
    

    좋은 웹페이지 즐겨찾기