spring boot 下對(duì)JSON返回值去除null和空字段操作
在開(kāi)發(fā)過(guò)程中,我們需要統(tǒng)一返回前端json格式的數(shù)據(jù),但有些接口的返回值存在 null或者""這種沒(méi)有意義的字段。
不僅影響理解,還浪費(fèi)帶寬,這時(shí)我們可以統(tǒng)一做一下處理,不返回空字段,或者把NULL轉(zhuǎn)成“”,spring 內(nèi)置的json處理框架是Jackson。我們可以對(duì)它配置一下達(dá)到目的
直接看代碼,很簡(jiǎn)單.
/** * 〈返回json空值去掉null和""〉 〈功能詳細(xì)描述〉 * * @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(); // 通過(guò)該方法對(duì)mapper對(duì)象進(jìn)行設(shè)置,所有序列化的對(duì)象都將按改規(guī)則進(jìn)行系列化 // Include.Include.ALWAYS 默認(rèn) // Include.NON_DEFAULT 屬性為默認(rèn)值不序列化 // Include.NON_EMPTY 屬性為 空("") 或者為 NULL 都不序列化,則返回的json是沒(méi)有這個(gè)字段的。這樣對(duì)移動(dòng)端會(huì)更省流量 // 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; } }
補(bǔ)充知識(shí):springboot RestController 配置fastjson,實(shí)體為null時(shí)不顯示問(wèn)題
Springboot 在和fastjson配合使用時(shí),當(dāng)返回實(shí)體為空時(shí)攔截不顯示問(wèn)題。在實(shí)際業(yè)務(wù)中,不管返回實(shí)體是否為空,都需要顯示出來(lái),如果為空則顯示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"); //處理中文亂碼問(wèn)題 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 }
解決問(wèn)題!
以上這篇spring boot 下對(duì)JSON返回值去除null和空字段操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring AOP中的JDK和CGLib動(dòng)態(tài)代理哪個(gè)效率更高?
今天小編就為大家分享一篇關(guān)于Spring AOP中的JDK和CGLib動(dòng)態(tài)代理哪個(gè)效率更高?,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)
通常來(lái)講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說(shuō)的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下2016-11-11java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù))
本篇文章主要介紹了java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類(lèi)型)
這篇文章主要介紹了SpringBoot調(diào)第三方WebService接口的操作代碼(.wsdl與.asmx類(lèi)型 ),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08java.sql.SQLException:?connection?holder?is?null錯(cuò)誤解決辦法
這篇文章主要給大家介紹了關(guān)于java.sql.SQLException:?connection?holder?is?null錯(cuò)誤的解決辦法,這個(gè)錯(cuò)誤通常是由于連接對(duì)象為空或未正確初始化導(dǎo)致的,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02