针对springboot查询的数据null转空串

jackson对null的处理

第一种方式:SpringBoot中使用Jackson将null值转化为""

在实际项目中难免会遇到null值的出现,但是我们转json时并不希望出现NULL值,而是将NULL值转化为 “” 这种空的字符串。那么,我们应该如何处理呢?在SpringBoot中,新建一个配置类即可。

@Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        }); return objectMapper;
    }
}

如此一来,遇到NULL值的时候,都会转化为""的形式。

第二种方式: 在yml文件中配置:

spring:
  jackson: default-property-inclusion: non_null

第三种方式: 在实体类上使用注解:

@JsonInclude(JsonInclude.Include.NON_NULL)

fastjson对null的处理

用到的jar有fastjson

这里用的版本是1.2.58

<!-- 阿里巴巴fastjson序列化 --> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> 

使用fastjson时,对null的处理和Jackson有些不同,需要继承WebMvcConfigurationSupport类,然后覆盖configureMessageConverters方法。在方法中,我们可以选择要实现null转换的场景,配置好即可。

import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @Configuration public class fastJsonConfig extends WebMvcConfigurationSupport { /**
     * 使用阿里 fastjson 作为 JSON MessageConverter
     * @param converters
     */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures( // 保留 Map 空的字段 SerializerFeature.WriteMapNullValue, // 将 String 类型的 null 转成"" SerializerFeature.WriteNullStringAsEmpty, // 将 Number 类型的 null 转成 0 SerializerFeature.WriteNullNumberAsZero, // 将 List 类型的 null 转成 [] SerializerFeature.WriteNullListAsEmpty, // 将 Boolean 类型的 null 转成 false SerializerFeature.WriteNullBooleanAsFalse, // 避免循环引用 SerializerFeature.DisableCircularReferenceDetect);

        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        List<MediaType> mediaTypeList = new ArrayList<>(); // 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json" mediaTypeList.add(MediaType.APPLICATION_JSON);
        converter.setSupportedMediaTypes(mediaTypeList);
        converters.add(converter);
    }
}
#Java##Spring#
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务