服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - ObjectMapper 如何忽略字段大小写

ObjectMapper 如何忽略字段大小写

2021-09-18 11:04程序边缘 Java教程

这篇文章主要介绍了使用ObjectMapper实现忽略字段大小写操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

ObjectMapper 忽略字段大小写

核心代码:

  1. ObjectMapper mapper = new ObjectMapper();
  2. mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  3. mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

例子:

  1. import com.fasterxml.jackson.databind.DeserializationFeature;
  2. import com.fasterxml.jackson.databind.JsonMappingException;
  3. import com.fasterxml.jackson.databind.MapperFeature;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. public class Test{
  6. public static void main(String[] args) {
  7. try {
  8. A a = new A();
  9. a.lastname = "jack";
  10. ObjectMapper mapper = new ObjectMapper();
  11. mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  12. mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
  13. A2 convertValue = new A2();
  14. mapper.updateValue(convertValue, a);
  15. System.out.println(convertValue);
  16. } catch (JsonMappingException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. public static class A{
  22. String lastname;
  23. public String getLastname() {
  24. return lastname;
  25. }
  26.  
  27. public void setLastname(String lastname) {
  28. this.lastname = lastname;
  29. }
  30. }
  31.  
  32. public static class A2{
  33. String lastName;
  34.  
  35. public String getLastName() {
  36. return lastName;
  37. }
  38.  
  39. public void setLastName(String lastName) {
  40. this.lastName = lastName;
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. return "A2 [lastName=" + lastName + "]";
  46. }
  47. }
  48. }

ObjectMapper 的一些坑

相信做过Java 开发对这个类应该不陌生,没错,这个类是jackson提供的,主要是用来把对象转换成为一个json字符串返回到前端,

现在大部分数据交换都是以json来传输的,所以这个很重要,那你到底又对这个类有着有多少了解呢,下面我说一下我遇到的一些坑

首先,先把我要说的几个坑需要设置的属性贴出来先

  1. ObjectMapper objectMapper = new ObjectMapper();
  2.  
  3. //序列化的时候序列对象的所有属性
  4. objectMapper.setSerializationInclusion(Include.ALWAYS);
  5.  
  6. //反序列化的时候如果多了其他属性,不抛出异常
  7. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  8.  
  9. //如果是空对象的时候,不抛异常
  10. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  11.  
  12. //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
  13. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  14. objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))

简单说一下这个类的基本用法,以下采用代码块加截图的形式来说明和部分文字件数

  1. package com.shiro.test;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  5. import com.fasterxml.jackson.databind.DeserializationFeature;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.SerializationFeature;
  8. public class Main2 {
  9. public static void main(String[] args) throws Exception{
  10. ObjectMapper objectMapper = new ObjectMapper();
  11. //序列化的时候序列对象的所有属性
  12. objectMapper.setSerializationInclusion(Include.ALWAYS);
  13. //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
  14. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  15. objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  16.  
  17. Person person = new Person(1, "zxc", new Date());
  18. //这是最简单的一个例子,把一个对象转换为json字符串
  19. String personJson = objectMapper.writeValueAsString(person);
  20. System.out.println(personJson);
  21.  
  22. //默认为true,会显示时间戳
  23. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
  24. personJson = objectMapper.writeValueAsString(person);
  25. System.out.println(personJson);
  26. }
  27. }

输出的信息如下

ObjectMapper 如何忽略字段大小写

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)的作用

  1. package com.shiro.test;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  5. import com.fasterxml.jackson.databind.DeserializationFeature;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.SerializationFeature;
  8. public class Main2 {
  9. public static void main(String[] args) throws Exception{
  10. ObjectMapper objectMapper = new ObjectMapper();
  11. //序列化的时候序列对象的所有属性
  12. objectMapper.setSerializationInclusion(Include.ALWAYS);
  13. //如果是空对象的时候,不抛异常,也就是对应的属性没有get方法
  14. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  15.  
  16. Person person = new Person(1, "zxc", new Date());
  17.  
  18. String personJson = objectMapper.writeValueAsString(person);
  19. System.out.println(personJson);
  20.  
  21. //默认是true,即会抛异常
  22. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
  23. personJson = objectMapper.writeValueAsString(person);
  24. System.out.println(personJson);
  25. }
  26. }

对应的person类此时为

  1. package com.shiro.test;
  2. import java.util.Date;
  3. public class Person {
  4. private Integer id;
  5. private String name;
  6. private Date birthDate;
  7. // public Integer getId() {
  8. // return id;
  9. // }
  10. // public void setId(Integer id) {
  11. // this.id = id;
  12. // }
  13. // public String getName() {
  14. // return name;
  15. // }
  16. // public void setName(String name) {
  17. // this.name = name;
  18. // }
  19. // public Date getBirthDate() {
  20. // return birthDate;
  21. // }
  22. // public void setBirthDate(Date birthDate) {
  23. // this.birthDate = birthDate;
  24. // }
  25. @Override
  26. public String toString() {
  27. return "Person [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]";
  28. }
  29. public Person(Integer id, String name, Date birthDate) {
  30. super();
  31. this.id = id;
  32. this.name = name;
  33. this.birthDate = birthDate;
  34. }
  35. public Person() {
  36. // TODO Auto-generated constructor stub
  37. }
  38. }

结果如下

ObjectMapper 如何忽略字段大小写

  1. package com.shiro.test;
  2. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. public class Main2 {
  6. public static void main(String[] args) throws Exception{
  7. ObjectMapper objectMapper = new ObjectMapper();
  8. //序列化的时候序列对象的所有属性
  9. objectMapper.setSerializationInclusion(Include.ALWAYS);
  10. //反序列化的时候如果多了其他属性,不抛出异常
  11. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  12.  
  13. // Person person = new Person(1, "zxc", new Date());
  14.  
  15. // String personJson = objectMapper.writeValueAsString(person);
  16. // System.out.println(personJson);
  17.  
  18. //注意,age属性是不存在在person对象中的
  19. String personStr = "{\"id\":1,\"name\":\"zxc\",\"age\":\"zxc\"}";
  20.  
  21. Person person = objectMapper.readValue(personStr, Person.class);
  22. System.out.println(person);
  23.  
  24. //默认为true
  25. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
  26. person = objectMapper.readValue(personStr, Person.class);
  27. System.out.println(person);
  28. }
  29. }

执行后的结果如下

ObjectMapper 如何忽略字段大小写

这些便是这几个属性的作用所以,由于第一个比较简单我就这样说一下吧

Include.ALWAYS 是序列化对像所有属性

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

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

然后再说一下如何把一个对象集合转换为一个 Java里面的数组

  1. package com.shiro.test;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  6. import com.fasterxml.jackson.core.type.TypeReference;
  7. import com.fasterxml.jackson.databind.JavaType;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. public class Main2 {
  10. public static void main(String[] args) throws Exception{
  11. ObjectMapper objectMapper = new ObjectMapper();
  12. //序列化的时候序列对象的所有属性
  13. objectMapper.setSerializationInclusion(Include.NON_DEFAULT);
  14.  
  15. Person person1 = new Person(1, "zxc", new Date());
  16. Person person2 = new Person(2, "ldh", new Date());
  17.  
  18. List<Person> persons = new ArrayList<>();
  19. persons.add(person1);
  20. persons.add(person2);
  21.  
  22. //先转换为json字符串
  23. String personStr = objectMapper.writeValueAsString(persons);
  24.  
  25. //反序列化为List<user> 集合,1需要通过 TypeReference 来具体传递值
  26. List<Person> persons2 = objectMapper.readValue(personStr, new TypeReference<List<Person>>() {});
  27.  
  28. for(Person person : persons2) {
  29. System.out.println(person);
  30. }
  31.  
  32. //2,通过 JavaType 来进行处理返回
  33. JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
  34. List<Person> persons3 = objectMapper.readValue(personStr, javaType);
  35.  
  36. for(Person person : persons3) {
  37. System.out.println(person);
  38. }
  39. }
  40. }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/weixin_42713970/article/details/88061100

延伸 · 阅读

精彩推荐