内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

SpringBoot項(xiàng)目中如何處理返回json的null值

這篇文章將為大家詳細(xì)講解有關(guān)SpringBoot項(xiàng)目中如何處理返回json的null值,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到義馬網(wǎng)站設(shè)計(jì)與義馬網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋義馬地區(qū)。

在后端數(shù)據(jù)接口項(xiàng)目開發(fā)中,經(jīng)常遇到返回的數(shù)據(jù)中有null值,導(dǎo)致前端需要進(jìn)行判斷處理,否則容易出現(xiàn)undefined的情況,如何便捷的將null值轉(zhuǎn)換為空字符串?

以SpringBoot項(xiàng)目為例,SSM同理。

1、新建配置類(JsonConfig.java)

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
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;
@Configuration
public class JsonConfig {
 @Bean
 @Primary
 @ConditionalOnMissingBean(ObjectMapper.class)
 public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
 {
  ObjectMapper objectMapper = builder.createXmlMapper(false).build();
  // 通過該方法對(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是沒有這個(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;
 }
}

2、在啟動(dòng)類Application中,記得添加Scan注解,防止無法掃描到配置類。

ps:下面看下spring boot 使用 json 響應(yīng)時(shí)去除 null 的字段

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
public class RespObject implements Serializable {
  private static final long serialVersionUID = -1560603887556641494L;
  ....
  @JsonInclude(Include.NON_NULL)
  public Object respMsg;
  @JsonInclude(Include.NON_NULL)
  public Object respData;
  ....
}

關(guān)于“SpringBoot項(xiàng)目中如何處理返回json的null值”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

本文題目:SpringBoot項(xiàng)目中如何處理返回json的null值
當(dāng)前路徑:http://www.rwnh.cn/article6/jeecog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃網(wǎng)站建設(shè)、域名注冊(cè)網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
蓬莱市| 西乌珠穆沁旗| 色达县| 平远县| 梧州市| 抚顺县| 石渠县| 虎林市| 教育| 且末县| 阿瓦提县| 陆河县| 宁陕县| 连南| 东山县| 西乌珠穆沁旗| 湘阴县| 佛坪县| 怀宁县| 沅江市| 房山区| 汤阴县| 阿克苏市| 周宁县| 长顺县| 龙南县| 昭平县| 灵山县| 红安县| 湘阴县| 嘉荫县| 平塘县| 河西区| 彭州市| 宁晋县| 武乡县| 铜鼓县| 珠海市| 渝北区| 隆林| 西藏|