springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例
在項(xiàng)目開(kāi)發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒(méi)有的時(shí)候一般直接返回的null
有數(shù)據(jù)時(shí)的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": "我的測(cè)試模板1",
"freightName": "我的測(cè)試標(biāo)題1",
"listArea": [
{
"id": 968,
"templateId": 32,
"freightPrice": 15,
}
],
"templateDescEntity": {
"id": 1
"name": "xxx"
}
}
}
沒(méi)有數(shù)據(jù)時(shí)的返回值
{
"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的時(shí)候就會(huì)直接報(bào)異常
此時(shí)我們可以使用返回值統(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類(lèi)添加配置
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í),設(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 {
//反射獲取字段類(lèi)型
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對(duì)象 返回{}
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)接口的各種參數(shù)校驗(yàn)的示例
- Springboot?接口需要接收參數(shù)類(lèi)型是數(shù)組問(wèn)題
- SpringBoot接口接收json參數(shù)解析
- SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)
- 在SpringBoot中使用@Value注解來(lái)設(shè)置默認(rèn)值的方法
- SpringBoot的@Value注解如何設(shè)置默認(rèn)值
- Springboot @Value注入boolean設(shè)置默認(rèn)值方式
- SpringBoot接口參數(shù)的默認(rèn)值與必要性最佳實(shí)踐記錄
相關(guān)文章
解決idea每次新建項(xiàng)目都需要重新指定maven目錄
這篇文章主要介紹了解決idea每次新建項(xiàng)目都需要配置maven,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
將本地JAR文件手動(dòng)添加到Maven本地倉(cāng)庫(kù)的實(shí)現(xiàn)過(guò)程
在Java開(kāi)發(fā)中,使用Maven作為項(xiàng)目管理工具已經(jīng)成為了主流的選擇,Maven提供了強(qiáng)大的依賴(lài)管理功能,可以輕松地下載和管理項(xiàng)目所需的庫(kù)和工具,在某些情況下,你可能會(huì)需要將本地下載的JAR文件手動(dòng)添加到Maven的本地倉(cāng)庫(kù)中,這篇博客將詳細(xì)介紹如何實(shí)現(xiàn)這一過(guò)程2024-10-10
解讀file.exists(),file.isFile()和file.isDirectory()的區(qū)別
本文介紹了Java中的File類(lèi)的三個(gè)方法:file.exists()、file.isFile()和file.isDirectory(),并詳細(xì)解釋了它們的區(qū)別和使用場(chǎng)景2025-02-02
spring boot 配置動(dòng)態(tài)刷新詳解
這篇文章主要介紹了spring boot 配置動(dòng)態(tài)刷新實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
Scala 操作Redis使用連接池工具類(lèi)RedisUtil
這篇文章主要介紹了Scala 操作Redis使用連接池工具類(lèi)RedisUtil,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Spring循環(huán)依賴(lài)產(chǎn)生與解決
Spring的解決循環(huán)依賴(lài)是有前置條件的,要解決循環(huán)依賴(lài)我們首先要了解Spring Bean對(duì)象的創(chuàng)建過(guò)程和依賴(lài)注入的方式。依賴(lài)注入方式,我之前的博客有所分享,大家可以在看本篇文章之前進(jìn)行一下小小的回顧2022-12-12

