ObjectMapper 如何忽略字段大小寫
ObjectMapper 忽略字段大小寫
核心代碼:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
例子:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test{
public static void main(String[] args) {
try {
A a = new A();
a.lastname = "jack";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
A2 convertValue = new A2();
mapper.updateValue(convertValue, a);
System.out.println(convertValue);
} catch (JsonMappingException e) {
e.printStackTrace();
}
}
public static class A{
String lastname;
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
public static class A2{
String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "A2 [lastName=" + lastName + "]";
}
}
}
ObjectMapper 的一些坑
相信做過Java 開發(fā)對(duì)這個(gè)類應(yīng)該不陌生,沒錯(cuò),這個(gè)類是jackson提供的,主要是用來把對(duì)象轉(zhuǎn)換成為一個(gè)json字符串返回到前端。
現(xiàn)在大部分?jǐn)?shù)據(jù)交換都是以json來傳輸?shù)?所以這個(gè)很重要,那你到底又對(duì)這個(gè)類有著有多少了解呢,下面我說一下我遇到的一些坑
首先,先把我要說的幾個(gè)坑需要設(shè)置的屬性貼出來先
ObjectMapper objectMapper = new ObjectMapper();
//序列化的時(shí)候序列對(duì)象的所有屬性
objectMapper.setSerializationInclusion(Include.ALWAYS);
//反序列化的時(shí)候如果多了其他屬性,不拋出異常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//如果是空對(duì)象的時(shí)候,不拋異常
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//取消時(shí)間的轉(zhuǎn)化格式,默認(rèn)是時(shí)間戳,可以取消,同時(shí)需要設(shè)置要表現(xiàn)的時(shí)間格式
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
簡單說一下這個(gè)類的基本用法,以下采用代碼塊加截圖的形式來說明和部分文字件數(shù)
package com.shiro.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class Main2 {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
//序列化的時(shí)候序列對(duì)象的所有屬性
objectMapper.setSerializationInclusion(Include.ALWAYS);
//取消時(shí)間的轉(zhuǎn)化格式,默認(rèn)是時(shí)間戳,可以取消,同時(shí)需要設(shè)置要表現(xiàn)的時(shí)間格式
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
Person person = new Person(1, "zxc", new Date());
//這是最簡單的一個(gè)例子,把一個(gè)對(duì)象轉(zhuǎn)換為json字符串
String personJson = objectMapper.writeValueAsString(person);
System.out.println(personJson);
//默認(rèn)為true,會(huì)顯示時(shí)間戳
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
personJson = objectMapper.writeValueAsString(person);
System.out.println(personJson);
}
}
輸出的信息如下

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)的作用
package com.shiro.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class Main2 {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
//序列化的時(shí)候序列對(duì)象的所有屬性
objectMapper.setSerializationInclusion(Include.ALWAYS);
//如果是空對(duì)象的時(shí)候,不拋異常,也就是對(duì)應(yīng)的屬性沒有g(shù)et方法
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Person person = new Person(1, "zxc", new Date());
String personJson = objectMapper.writeValueAsString(person);
System.out.println(personJson);
//默認(rèn)是true,即會(huì)拋異常
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
personJson = objectMapper.writeValueAsString(person);
System.out.println(personJson);
}
}
對(duì)應(yīng)的person類此時(shí)為
package com.shiro.test;
import java.util.Date;
public class Person {
private Integer id;
private String name;
private Date birthDate;
// public Integer getId() {
// return id;
// }
// public void setId(Integer id) {
// this.id = id;
// }
// public String getName() {
// return name;
// }
// public void setName(String name) {
// this.name = name;
// }
// public Date getBirthDate() {
// return birthDate;
// }
// public void setBirthDate(Date birthDate) {
// this.birthDate = birthDate;
// }
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]";
}
public Person(Integer id, String name, Date birthDate) {
super();
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public Person() {
// TODO Auto-generated constructor stub
}
}
結(jié)果如下

package com.shiro.test;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main2 {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
//序列化的時(shí)候序列對(duì)象的所有屬性
objectMapper.setSerializationInclusion(Include.ALWAYS);
//反序列化的時(shí)候如果多了其他屬性,不拋出異常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Person person = new Person(1, "zxc", new Date());
// String personJson = objectMapper.writeValueAsString(person);
// System.out.println(personJson);
//注意,age屬性是不存在在person對(duì)象中的
String personStr = "{\"id\":1,\"name\":\"zxc\",\"age\":\"zxc\"}";
Person person = objectMapper.readValue(personStr, Person.class);
System.out.println(person);
//默認(rèn)為true
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
person = objectMapper.readValue(personStr, Person.class);
System.out.println(person);
}
}
執(zhí)行后的結(jié)果如下

這些便是這幾個(gè)屬性的作用所以,由于第一個(gè)比較簡單我就這樣說一下吧
Include.ALWAYS 是序列化對(duì)像所有屬性
Include.NON_NULL 只有不為null的字段才被序列化
Include.NON_EMPTY 如果為null或者 空字符串和空集合都不會(huì)被序列化
然后再說一下如何把一個(gè)對(duì)象集合轉(zhuǎn)換為一個(gè) Java里面的數(shù)組
package com.shiro.test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main2 {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
//序列化的時(shí)候序列對(duì)象的所有屬性
objectMapper.setSerializationInclusion(Include.NON_DEFAULT);
Person person1 = new Person(1, "zxc", new Date());
Person person2 = new Person(2, "ldh", new Date());
List<Person> persons = new ArrayList<>();
persons.add(person1);
persons.add(person2);
//先轉(zhuǎn)換為json字符串
String personStr = objectMapper.writeValueAsString(persons);
//反序列化為List<user> 集合,1需要通過 TypeReference 來具體傳遞值
List<Person> persons2 = objectMapper.readValue(personStr, new TypeReference<List<Person>>() {});
for(Person person : persons2) {
System.out.println(person);
}
//2,通過 JavaType 來進(jìn)行處理返回
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
List<Person> persons3 = objectMapper.readValue(personStr, javaType);
for(Person person : persons3) {
System.out.println(person);
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java簡單實(shí)現(xiàn)農(nóng)夫過河問題示例
這篇文章主要介紹了Java簡單實(shí)現(xiàn)農(nóng)夫過河問題,簡單描述了農(nóng)夫過河問題的概念、原理并結(jié)合簡單實(shí)例形式分析了java解決農(nóng)夫過河問題的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
java實(shí)現(xiàn)時(shí)間與字符串之間轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)時(shí)間與字符串之間轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
java中的FileInputStream三種read()函數(shù)用法
這篇文章主要介紹了java中的FileInputStream三種read()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java根據(jù)控制臺(tái)實(shí)現(xiàn)定位異常
這篇文章主要介紹了Java根據(jù)控制臺(tái)定位異常,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
應(yīng)用Java泛型和反射導(dǎo)出CSV文件的方法
這篇文章主要介紹了應(yīng)用Java泛型和反射導(dǎo)出CSV文件的方法,通過一個(gè)自定義函數(shù)結(jié)合泛型與反射的應(yīng)用實(shí)現(xiàn)導(dǎo)出CSV文件的功能,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
MyBatisPlus中使用or()和and()遇到的問題及細(xì)節(jié)處理
這篇文章主要介紹了MyBatisPlus中使用or()和and()遇到的問題,本文通過多種寫法實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

