1. 程式人生 > >spring mvc 返回json中去除null欄位

spring mvc 返回json中去除null欄位

為了減少不必要的內容傳輸, 過濾null欄位,可對返回的類加入註釋來解決。

條件:

1.  spring MVC 3.X 支援註解

2. jackson-code.jar,jackson-mapper.jar 通常用jackson-all-xxx.jar 就行了

使用:

在需要序列化為json輸出的類上增加, 例如:

import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class XXXX{
}

官方說明

@deprecated As of Jackson 2.0, this annotation has been replaced
     *    by {@link com.fasterxml.jackson.annotation.JsonInclude}

改成:

@JsonInclude(Include.NON_NULL)
public class XXXX{
}

如果要全域性配置可參考:

<mvc:message-converters register-defaults="true">
			<bean
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="prefixJson" value="false" />
				<property name="supportedMediaTypes" value="application/json" />
				<property name="objectMapper">
					<bean class="com.fasterxml.jackson.databind.ObjectMapper">
						<property name="serializationInclusion">
							<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
						</property>
					</bean>
				</property>
			</bean>
		</mvc:message-converters>

<mvc:annotation-driven>
	    <!-- 強化引數接收 -->
		<mvc:argument-resolvers>
			<bean class="org.durcframework.common.form.FormModelMethodArgumentResolver" />
		</mvc:argument-resolvers>
		 
        <mvc:message-converters>  
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="objectMapper">  
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    	<!-- 處理responseBody 裡面日期型別 -->  
                        <property name="dateFormat">  
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property> 
                        <!-- 為null欄位時不顯示 -->
                        <property name="serializationInclusion">
                            <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                        </property> 
                    </bean>  
                </property>  
            </bean>  
        </mvc:message-converters>  
	</mvc:annotation-driven>
全域性配置有個問題是,本來就該為null值,在結果中就找不到了,不利於除錯。

相關推薦

spring mvc 返回json去除null

為了減少不必要的內容傳輸, 過濾null欄位,可對返回的類加入註釋來解決。 條件: 1.  spring MVC 3.X 支援註解 2. jackson-code.jar,jackson-mapper.jar 通常用jackson-all-xxx.jar 就行

spring mvc 返回JSON數據

img iat console lips 12px array mediatype cnblogs ota servlet相關XML配置 <!--這個不啟用會報錯--><mvc:annotation-driven />

Spring MVC 返回JSON

property 客戶 media true jackson nbsp 實體類 servle 類集 JSON是一種通用的傳輸數據的格式,現在處理JSON數據的jar包有很多,比如Jackson,Gson等,大部分方法都類似,無非是將實體類-JSON互相轉換。 在Spring

spring mvc返回json字串的方式

 spring mvc返回json字串的方式 方案一:使用@ResponseBody 註解返回響應體 直接將返回值序列化json            優點:不需要自己再處理 步驟一:

Spring MVC 返回 JSON資料

兩種方式: 第一種:使用阿里的fastjson,對應的包。 首先,在pox.xml新增jar包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjs

springmvc返回json資料去除null

package com.xilehang.kaola.po.orderConfirm; import com.fasterxml.jackson.databind.annotation.JsonSerialize; //轉json是排除null @SuppressWar

Spring MVC返回json資料的配置方式

摘要:Spring MVC返回json資料的方式有以下幾種: (1)直接 PrintWriter 輸出 (2)使用 JSP 檢視 (3)使用Spring內建的支援本文說明的是使用Spring內建的功能。 Spring MVC返回json資料的方式有以下幾種:     (1

spring mvc 返回json 時間格式處理

從後臺返回json格式的資料,timestamp 與datetime 型別的資料,在前臺顯示為long型別的, 很是苦惱, 研究了很長時間,下面總結一下: SpringMVC使用@ResponseBody時返回json的日期格式      前提了解: @Resp

Spring MVC 返回Json數據環境記錄

分享 pri not 返回JSON數據 環境 版本 返回json 4.3 spring4 Spring 版本 Spring4.3.18 Json包 jackson-annotations-2.9.8.jar jackson-core-2.9.

idea ssm專案出現日誌中文亂碼,封裝的json的msg中文亂碼(但是json封裝的bean不亂碼)等其他各種專案下的中文亂碼解決方案

   開頭劃重點!(敲黑板):rebuild和mvn package的迴圈往復好幾次的操作是解決這個問題的最主要的方法! 經過多次試驗,發現這樣做就可以正常顯示中文了 我說為什麼有時候亂碼,有時候中文正常,原來下面的這些地方都必須設定好,特別是 rebuild和m

如何理解 package.json 的 proxy

入職新公司以來,第一個月接手vue專案,第二個月接手angularjs專案,第三個月加入react重構專案。心生感嘆:業務驅動式學習是一種高效率的學習方式,保持好奇心,在業務中快速成長! 新專案中在package.json中有一個proxy欄位,這是我從來沒接觸過的,因此就有了此文的誕

spring boot mongodb 統計表某一的所有值,其中aggregate的用法之一

pom.xml 依賴: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb<

Spring Boot + MyBatis 電商專案 -04- 使用 Spring MVC 讀取使用者資訊、篩選

Spring Boot + MyBatis 電商專案 -04- 使用 Spring MVC 讀取使用者資訊、篩選欄位 前幾篇文章: (如果不是新手,建議至少看完第 3 篇,因為整個專案是基於第三篇建立的很多檔案進行的) Spring Boot + MyBatis 電商

SpringMVC 用JackJson做介面返回JSON格式指定的解決方法

最近一個同事在寫一個介面,其中要返回的是一個物件的List,最後是以json格式的字串給介面呼叫方, 於是就出現一個問題,因為這個物件的欄位很多,但是介面呼叫方卻不需要那麼多,當時想新建一個類去做,不過萬一有很多介面,這樣就比較麻煩 ,於是到網上找了找返現有很多方法可以

Spring mvc無法接受到大資料處理

1 :Spring mvc 用@RequestBody 方式,接收方法如下: @RequestMapping(value = "testRecieve") @ResponseBod

Gson解析JSON動態未知key的方法

       前面一篇文章我介紹了Gson的解析的基本方法。但我們在享受Gson解析的高度封裝帶來的便利時,有時可能會遇到一些特殊情況,比如json資料中的欄位key是動態可變的時候,由於Gson是

Spring MVC 更靈活的控制 json 返回(自定義過濾

前言: 我們要做什麼?         我們要在springmvc的控制層,在返回字串的時候,動態改變一下到底要返回哪些字串,比如我們要返回實體Article,他裡面有4個屬性我們只想返回其中的3個

spring mvc 返回json去掉null

在springmvc.xml中新增如下: <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <prop

Spring MVC返回多重的Json數據

eat empty keywords set nes string arraylist shm spa 一、需求: 頁面返回數據 1 { 2 "code": 0, 3 "msg": "", 4 "count": "2", 5 "data": [{

Spring MVC返回Map格式JSON資料 瀏覽器報406 錯誤

最近嘗試著部署ssm專案,登入功能驗證使用者登入資訊返回驗證結果的時候,前臺是用ajax請求。 前臺請求ajax程式碼如下: 後臺controller 前臺請求和controller都沒錯,tomcat 也沒有報錯,匯入了jackson-core-asl-1.9.13+j