spring-boot返回資料為null時返回空字串
阿新 • • 發佈:2019-01-04
在專案開發中,我們會遇到一些null值。如果不想讓返回資料有null值,而是返回空字串該怎麼辦捏。
So Easy!!!
我們做一下配置就好了。
1.Jackson null值處理:spring-boot是對Jackson預設支援的,所以有了web的依賴則不需要引入依賴了。
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; 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.io.IOException; /** * 描述: 處理資料返回的null值 * @author Administrator * @create 2018-10-18 14:51 */ @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; } }
2.fastjson null值處理
2.1:需要引入fastjson 依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
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); } }
ok,這樣就可以將返回資料中的null值轉為空字串了。