spring boot 下對JSON返回值去除null和空字段操作
在開發(fā)過程中,我們需要統(tǒng)一返回前端json格式的數(shù)據(jù),但有些接口的返回值存在 null或者""這種沒有意義的字段。
不僅影響理解,還浪費帶寬,這時我們可以統(tǒng)一做一下處理,不返回空字段,或者把NULL轉(zhuǎn)成“”,spring 內(nèi)置的json處理框架是Jackson。我們可以對它配置一下達到目的
直接看代碼,很簡單.
/**
* 〈返回json空值去掉null和""〉 〈功能詳細描述〉
*
* @author gogym
* @version 2017年10月13日
* @see JacksonConfig
* @since
*/
@Configuration
public class JacksonConfig
{
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
{
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 通過該方法對mapper對象進行設(shè)置,所有序列化的對象都將按改規(guī)則進行系列化
// Include.Include.ALWAYS 默認
// Include.NON_DEFAULT 屬性為默認值不序列化
// Include.NON_EMPTY 屬性為 空("") 或者為 NULL 都不序列化,則返回的json是沒有這個字段的。這樣對移動端會更省流量
// Include.NON_NULL 屬性為NULL 不序列化,就是為null的字段不參加序列化
//objectMapper.setSerializationInclusion(Include.NON_EMPTY);
// 字段保留,將null值轉(zhuǎn)為""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object o, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException
{
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
補充知識:springboot RestController 配置fastjson,實體為null時不顯示問題
Springboot 在和fastjson配合使用時,當返回實體為空時攔截不顯示問題。在實際業(yè)務(wù)中,不管返回實體是否為空,都需要顯示出來,如果為空則顯示null。
解決方案,引入fastjson jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
添加配置ResultConfig:
package com.message.config;
/**
* @author :zoboy
* @Description:
* @ Date: Created in 2019-11-18 10:29
*/
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class ResultConfig {
/*注入Bean : HttpMessageConverters,以支持fastjson*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.DisableCheckSpecialChar);
fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");
//處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConvert.setSupportedMediaTypes(fastMediaTypes);
fastConvert.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters((HttpMessageConverter<?>) fastConvert);
}
}
結(jié)果:
{
"code": "0",
"message": "成功!",
"data": null
}
解決問題!
以上這篇spring boot 下對JSON返回值去除null和空字段操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA里找不到j(luò)avax.servlet的jar包的解決方法
這篇文章主要介紹了IntelliJ IDEA里找不到j(luò)avax.servlet的jar包的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
解決Idea的選擇文件后定位瞄準器"Select Opened File"的功能
使用IntelliJ IDEA時,可能會發(fā)現(xiàn)"SelectOpenedFile"功能不見了,這個功能允許用戶快速定位到當前打開文件的位置,若要找回此功能,只需在IDEA的標題欄上右鍵,然后選擇"Always Select Opened File",這樣就可以重新啟用這個便捷的功能2024-11-11
Java使用NIO包實現(xiàn)Socket通信的實例代碼
本篇文章主要介紹了Java使用NIO包實現(xiàn)Socket通信的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

