溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ObjectMapper怎么忽略字段大小寫

發(fā)布時間:2021-06-29 14:45:50 來源:億速云 閱讀:405 作者:chen 欄目:開發(fā)技術

本篇內容主要講解“ObjectMapper怎么忽略字段大小寫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“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ā)對這個類應該不陌生,沒錯,這個類是jackson提供的,主要是用來把對象轉換成為一個json字符串返回到前端,

現(xiàn)在大部分數據交換都是以json來傳輸的,所以這個很重要,那你到底又對這個類有著有多少了解呢,下面我說一下我遇到的一些坑

首先,先把我要說的幾個坑需要設置的屬性貼出來先

ObjectMapper objectMapper = new ObjectMapper();
  
  //序列化的時候序列對象的所有屬性
  objectMapper.setSerializationInclusion(Include.ALWAYS);
  
  //反序列化的時候如果多了其他屬性,不拋出異常
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  
  //如果是空對象的時候,不拋異常
  objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  
  //取消時間的轉化格式,默認是時間戳,可以取消,同時需要設置要表現(xiàn)的時間格式
  objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))

簡單說一下這個類的基本用法,以下采用代碼塊加截圖的形式來說明和部分文字件數

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();
  //序列化的時候序列對象的所有屬性
  objectMapper.setSerializationInclusion(Include.ALWAYS);
  //取消時間的轉化格式,默認是時間戳,可以取消,同時需要設置要表現(xiàn)的時間格式
  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());
  //這是最簡單的一個例子,把一個對象轉換為json字符串
  String personJson = objectMapper.writeValueAsString(person);
  System.out.println(personJson);
  
  //默認為true,會顯示時間戳
  objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
  personJson = objectMapper.writeValueAsString(person);
  System.out.println(personJson);
 }
}

輸出的信息如下

ObjectMapper怎么忽略字段大小寫

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();
		//序列化的時候序列對象的所有屬性
		objectMapper.setSerializationInclusion(Include.ALWAYS);
		//如果是空對象的時候,不拋異常,也就是對應的屬性沒有get方法
		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);
		
		//默認是true,即會拋異常
		objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
		personJson = objectMapper.writeValueAsString(person);
		System.out.println(personJson);	
	}
}

對應的person類此時為

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
	}
}

結果如下

ObjectMapper怎么忽略字段大小寫

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();
		//序列化的時候序列對象的所有屬性
		objectMapper.setSerializationInclusion(Include.ALWAYS);
		//反序列化的時候如果多了其他屬性,不拋出異常
		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對象中的
		String personStr = "{\"id\":1,\"name\":\"zxc\",\"age\":\"zxc\"}";
		
		Person person = objectMapper.readValue(personStr, Person.class);
		System.out.println(person);
		
		//默認為true
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
		person = objectMapper.readValue(personStr, Person.class);
		System.out.println(person);		
	}
}

執(zhí)行后的結果如下

ObjectMapper怎么忽略字段大小寫

這些便是這幾個屬性的作用所以,由于第一個比較簡單我就這樣說一下吧

Include.ALWAYS 是序列化對像所有屬性

Include.NON_NULL 只有不為null的字段才被序列化

Include.NON_EMPTY 如果為null或者 空字符串和空集合都不會被序列化

然后再說一下如何把一個對象集合轉換為一個 Java里面的數組

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();
		//序列化的時候序列對象的所有屬性
		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);
		
		//先轉換為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 來進行處理返回
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
		List<Person> persons3 = objectMapper.readValue(personStr, javaType);
		
		for(Person person : persons3) {
			System.out.println(person);
		}
	}
}

到此,相信大家對“ObjectMapper怎么忽略字段大小寫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI