springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例
在項(xiàng)目開發(fā)中,我們返回的數(shù)據(jù)或者對象沒有的時候一般直接返回的null
有數(shù)據(jù)時的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": "我的測試模板1",
"freightName": "我的測試標(biāo)題1",
"listArea": [
{
"id": 968,
"templateId": 32,
"freightPrice": 15,
}
],
"templateDescEntity": {
"id": 1
"name": "xxx"
}
}
}
沒有數(shù)據(jù)時的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": null,
"freightName": null,
"listArea": null,
"templateDescEntity": null
}
}
這種情況下數(shù)據(jù)返回給前端,前端需要做大量的空值判斷
如前端調(diào)使用屬性data.templateDescEntity.id的時候就會直接報(bào)異常
此時我們可以使用返回值統(tǒng)一處理,配置如下
pom.xml添加
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
java類添加配置
package com.ys.mall.core.product.config;
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;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 數(shù)據(jù)返回給前端時,設(shè)置null值默認(rèn)為""
*
* @author cgh
* @date 2020/12/14 10:35
*/
@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 {
String fieldName = jsonGenerator.getOutputContext().getCurrentName();
try {
//反射獲取字段類型
Field field = jsonGenerator.getCurrentValue().getClass().getDeclaredField(fieldName);
if (CharSequence.class.isAssignableFrom(field.getType())) {
//字符串型空值""
jsonGenerator.writeString("");
return;
} else if (Collection.class.isAssignableFrom(field.getType())) {
//列表型空值返回[]
jsonGenerator.writeStartArray();
jsonGenerator.writeEndArray();
return;
} else if (Map.class.isAssignableFrom(field.getType())) {
//map型空值 或者 bean對象 返回{}
jsonGenerator.writeStartObject();
jsonGenerator.writeEndObject();
return;
}
} catch (NoSuchFieldException ignored) {
}
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
添加空值統(tǒng)一處理后的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": "",
"freightName": "",
"listArea": [],
"templateDescEntity": {}
}
}
到此這篇關(guān)于springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot Jackson返回統(tǒng)一默認(rèn)值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)接口的各種參數(shù)校驗(yàn)的示例
- Springboot?接口需要接收參數(shù)類型是數(shù)組問題
- SpringBoot接口接收json參數(shù)解析
- SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)
- 在SpringBoot中使用@Value注解來設(shè)置默認(rèn)值的方法
- SpringBoot的@Value注解如何設(shè)置默認(rèn)值
- Springboot @Value注入boolean設(shè)置默認(rèn)值方式
- SpringBoot接口參數(shù)的默認(rèn)值與必要性最佳實(shí)踐記錄
相關(guān)文章
idea sql的xml文件出現(xiàn)紅色警告符的處理方式
這篇文章主要介紹了idea sql的xml文件出現(xiàn)紅色警告符處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見的HTTP請求數(shù)據(jù)格式,它將請求參數(shù)編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05
Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式
這篇文章主要介紹了Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式,JAVA反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個實(shí)體類,都能夠知道這個類的所有屬性和方法,需要的朋友可以參考下2023-10-10
SpringBoot2.x 整合 AntiSamy防御XSS攻擊的簡單總結(jié)
本文主要對SpringBoot2.x集成AntiSamy防御XSS攻擊進(jìn)行簡單總結(jié),其中SpringBoot使用的2.4.5版本,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作
這篇文章主要介紹了Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

