av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

springboot配置Jackson返回統(tǒng)一默認(rèn)值的實現(xiàn)示例

瀏覽:39日期:2023-02-21 16:12:42

在項目開發(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的時候就會直接報異常此時我們可以使用返回值統(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 */@Configurationpublic 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)值的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot Jackson返回統(tǒng)一默認(rèn)值內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美激情在线精品一区二区三区 | 久久电影一区 | 一区二区三区四区在线播放 | 欧美日韩综合一区 | 午夜精品三区 | 中文字幕一区二区三 | 天天操夜夜拍 | 欧美综合久久 | 在线观看亚洲精品视频 | 91欧美激情一区二区三区成人 | 亚洲色图在线观看 | 免费观看的av毛片的网站 | 色综合一区二区 | 亚洲精品久久区二区三区蜜桃臀 | 久久久精品影院 | 97人人爱 | 成人在线观看网站 | 国产成人精品一区二区三 | 欧美成人一区二区 | 久久亚洲国产精品 | 7799精品视频天天看 | 久久高清国产视频 | 一区二区欧美在线 | 久久久久99 | 欧美日韩精选 | 一区二区三区av | 欧美在线视频一区 | 亚洲精品福利在线 | 一a一片一级一片啪啪 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 免费小视频在线观看 | 欧美精品一区二区免费 | 日本一本在线 | 女同久久另类99精品国产 | 亚洲欧美一区二区三区国产精品 | 欧美成人a | 亚洲一区二区视频 | 亚洲人免费视频 | 国产成人网 | 91夜色在线观看 | 激情毛片|