對(duì)象轉(zhuǎn)Json字符串時(shí)如何忽略指定屬性
FastJson轉(zhuǎn)Json字符串時(shí),忽略指定屬性
使用注解@JSONField
以下Bean
package com.gomefinance.esign;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* 本地簽署信息
* Created by JHAO on 2017/5/31.
*/
@Setter
@Getter
public class FastJsonInputBean implements Serializable {
// 合同模板ID
private String contractTemplateId;
// 合同號(hào)
private String contractId;
// Base64編碼的合同
@JSONField(serialize = false)
private String contractImage;
private String contractVersion;
} FastJsonInputBean inputBean = new FastJsonInputBean();
inputBean.setContractImage("contractImage");
inputBean.setContractTemplateId("templateId");
inputBean.setContractId("comtractId");
inputBean.setContractVersion("contractVersion");
System.out.println("JSONField(serialize = false)忽略contractImage屬性:"+JSON.toJSONString(inputBean));打印結(jié)果:
{"contractId":"comtractId","contractTemplateId":"templateId","contractVersion":"contractVersion"}
Filter指定序列化的字段
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(FastJsonInputBean.class, "contractTemplateId");
System.out.println("filter忽略contractTemplateId屬性:"+JSONObject.toJSONString(inputBean, filter));?
打印結(jié)果:
{"contractTemplateId":"templateId"}
JackSon忽略字段
@JsonIgnoreProperties主鍵或者在字段上使用@JsonIgnore
package com.gomefinance.esign;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* 本地簽署信息
* Created by JHAO on 2017/5/31.
*/
@Setter
@Getter
@JsonIgnoreProperties({"contractTemplateId", "contractImage"})
public class JackSonInputBean implements Serializable {
// 合同模板ID
private String contractTemplateId;
// 合同號(hào)
@JsonIgnore
private String contractId;
// Base64編碼的合同
private String contractImage;
private String contractVersion;
} JackSonInputBean jackSonInputBean = new JackSonInputBean();
jackSonInputBean.setContractImage("contractImage");
jackSonInputBean.setContractTemplateId("templateId");
jackSonInputBean.setContractId("comtractId");
jackSonInputBean.setContractVersion("contractVersion");
ObjectMapper objectMapper = new ObjectMapper();
System.out.println("JackSon JsonIgnore忽略contractId屬性:"+objectMapper.writeValueAsString(jackSonInputBean));打印結(jié)果:
{"contractVersion":"contractVersion"}
Jackson Json與對(duì)象轉(zhuǎn)換的幾個(gè)配置筆記
最近因?yàn)轫?xiàng)目需要,對(duì)Jackson的使用進(jìn)行了深入的學(xué)習(xí),總結(jié)了幾條使用心得,在此記錄一下:
在做Json字符串轉(zhuǎn)換為對(duì)象時(shí),有時(shí)會(huì)出現(xiàn)對(duì)象中存在某屬性,而json字符串沒有此屬性,缺省情況下會(huì)拋出異常。
可通過以下兩種方式配置不拋出異常:
- application.properties中進(jìn)行全局配置:spring.jackson.deserialization.fail_on_unknown_properties=false
- 針對(duì)需要配置對(duì)象類,添加注解: @JsonIgnoreProperties(ignoreUnknown = true)
java類的屬性,一般是小駝峰模式,如testStr。在轉(zhuǎn)換為Json字符串時(shí),可以控制json中屬性的命名方式,如大駝峰,小駝峰,蛇形(下劃線),不變等。
全局配置屬性spring.jackson.property-naming-strategy
UPPER_CAMEL_CASE- 大駝峰 (TestStr)LOWER_CAMEL_CASE- 小駝峰 (testStr)SNAKE_CASE- 下劃線 (test_str)LOWER_CASE- 小寫 (teststr)KEBAB_CASE- 減號(hào) (test-str)
針對(duì)指定對(duì)象,添加注解@JsonNaming(PropertyNamingStrategy.???.class)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)- 大駝峰@JsonNaming(PropertyNamingStrategy.LowerDotCaseStrategy.class)- .分隔 test.str@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)- 下劃線@JsonNaming(PropertyNamingStrategy.LowerCaseStrategy.class)- 小寫@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class)- 減號(hào)
其中有一點(diǎn)需要注意,對(duì)于從json字符串轉(zhuǎn)換為實(shí)體對(duì)象時(shí),即使設(shè)置了大駝峰等模式,使用對(duì)象屬性名,也可以解析成功,即TestStr, testStr均可解析成功。
空屬性是否序列化
全局配置spring.jackson.default-property-inclusion=non_null
non_null: null不解析non_empty: 空字符’'和null均不解析
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot如何通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置
這篇文章主要介紹了Spring boot如何通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java讀取某個(gè)文件夾下的所有文件實(shí)例代碼
這篇文章主要介紹了java讀取某個(gè)文件夾下的所有文件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
IDEA里找不到Maven的有效解決辦法(小白超詳細(xì))
這篇文章主要給大家介紹了關(guān)于IDEA里找不到Maven的有效解決辦法,文中通過圖文將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter
這篇文章主要介紹了springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06

