ObjectMapper 如何忽略字段大小写


Posted in Java/Android onJune 29, 2021

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 开发对这个类应该不陌生,没错,这个类是jackson提供的,主要是用来把对象转换成为一个json字符串返回到前端,

现在大部分数据交换都是以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);
  
  //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
  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);
  //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
  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);		
	}
}

执行后的结果如下

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Java/Android 相关文章推荐
浅析NIO系列之TCP
Jun 15 Java/Android
Feign调用传输文件异常的解决
Jun 24 Java/Android
一篇文章带你学习Mybatis-Plus(新手入门)
Aug 02 Java/Android
使用logback实现按自己的需求打印日志到自定义的文件里
Aug 30 Java/Android
关于Spring配置文件加载方式变化引发的异常详解
Jan 18 Java/Android
Java生成日期时间存入Mysql数据库的实现方法
Mar 03 Java/Android
Spring事务管理下synchronized锁失效问题的解决方法
Mar 31 Java/Android
Spring Data JPA框架自定义Repository接口
Apr 28 Java/Android
Java 数组的使用
May 11 Java/Android
前端与RabbitMQ实时消息推送未读消息小红点实现示例
Jul 23 Java/Android
Spring boot admin 服务监控利器详解
Aug 05 Java/Android
Android实现获取短信验证码并自动填充
May 21 Java/Android
Java常用函数式接口总结
分析并发编程之LongAdder原理
SpringBoot整合JWT的入门指南
jackson json序列化实现首字母大写,第二个字母需小写
Java数组与堆栈相关知识总结
分析JVM源码之Thread.interrupt系统级别线程打断
Jun 29 #Java/Android
Jackson 反序列化时实现大小写不敏感设置
Jun 29 #Java/Android
You might like
PHP初学者常见问题集合 修正版(21问答)
2010/03/23 PHP
PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库
2015/11/25 PHP
PHP simplexml_import_dom()函数讲解
2019/02/03 PHP
JavaScript实现表格排序方法
2013/06/14 Javascript
jQuery 属性选择器element[herf*='value']使用示例
2013/10/20 Javascript
js中的时间转换—毫秒转换成日期时间的示例代码
2014/01/26 Javascript
javascript模拟实现ajax加载框实例
2014/10/15 Javascript
jQuery toggle 代替方法
2016/03/22 Javascript
JS控制伪元素的方法汇总
2016/04/06 Javascript
Nodejs获取网络数据并生成Excel表格
2020/03/31 NodeJs
JavaScript随机生成颜色的方法
2016/10/15 Javascript
Bootstrap基本插件学习笔记之标签切换(17)
2016/12/08 Javascript
jQuery Validate让普通按钮触发表单验证的方法
2016/12/15 Javascript
js禁止浏览器页面后退功能的实例(推荐)
2017/09/01 Javascript
Vue Cli3 创建项目的方法步骤
2018/10/15 Javascript
JavaScript常用工具方法封装
2019/02/12 Javascript
python实现数独游戏 java简单实现数独游戏
2018/03/30 Python
DataFrame中的object转换成float的方法
2018/04/10 Python
基于Python List的赋值方法
2018/06/23 Python
详解Python字典小结
2018/10/20 Python
python实现ip地址查询经纬度定位详解
2019/08/30 Python
50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)
2019/11/20 Python
django自定义模板标签过程解析
2019/12/14 Python
Python绘制动态水球图过程详解
2020/06/03 Python
python 基于opencv 绘制图像轮廓
2020/12/11 Python
CSS3 box-shadow属性实例详解
2020/06/19 HTML / CSS
Java里面StringBuilder和StringBuffer有什么区别
2016/06/06 面试题
用缩写的指针比较"if(p)" 检查空指针是否可靠?如果空指针的内部表达不是0会怎么样?
2014/01/05 面试题
绘画设计学生的个人自我评价
2013/09/20 职场文书
廉洁家庭事迹材料
2014/05/15 职场文书
音乐幼师求职信
2014/07/09 职场文书
人力资源管理专业求职信
2014/07/23 职场文书
2014年国庆节演讲稿
2014/09/19 职场文书
民间借贷协议书范本
2014/10/01 职场文书
2014年英语教师工作总结
2014/12/03 职场文书
2016年党员干部廉政承诺书
2016/03/24 职场文书