ObjectMapper 如何忽略字段大小写

2025-05-29 0 70

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. public static class A{
  21. String lastname;
  22. public String getLastname() {
  23. return lastname;
  24. }
  25. public void setLastname(String lastname) {
  26. this.lastname = lastname;
  27. }
  28. }
  29. public static class A2{
  30. String lastName;
  31. public String getLastName() {
  32. return lastName;
  33. }
  34. public void setLastName(String lastName) {
  35. this.lastName = lastName;
  36. }
  37. @Override
  38. public String toString() {
  39. return "A2 [lastName=" + lastName + "]";
  40. }
  41. }
  42. }

ObjectMapper 的一些坑

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

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

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

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. //序列化的时候序列对象的所有属性
  3. objectMapper.setSerializationInclusion(Include.ALWAYS);
  4. //反序列化的时候如果多了其他属性,不抛出异常
  5. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  6. //如果是空对象的时候,不抛异常
  7. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  8. //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
  9. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  10. 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. Person person = new Person(1, "zxc", new Date());
  17. //这是最简单的一个例子,把一个对象转换为json字符串
  18. String personJson = objectMapper.writeValueAsString(person);
  19. System.out.println(personJson);
  20. //默认为true,会显示时间戳
  21. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
  22. personJson = objectMapper.writeValueAsString(person);
  23. System.out.println(personJson);
  24. }
  25. }

输出的信息如下

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. Person person = new Person(1, "zxc", new Date());
  16. String personJson = objectMapper.writeValueAsString(person);
  17. System.out.println(personJson);
  18. //默认是true,即会抛异常
  19. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
  20. personJson = objectMapper.writeValueAsString(person);
  21. System.out.println(personJson);
  22. }
  23. }

对应的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. // Person person = new Person(1, "zxc", new Date());
  13. // String personJson = objectMapper.writeValueAsString(person);
  14. // System.out.println(personJson);
  15. //注意,age属性是不存在在person对象中的
  16. String personStr = "{\\"id\\":1,\\"name\\":\\"zxc\\",\\"age\\":\\"zxc\\"}";
  17. Person person = objectMapper.readValue(personStr, Person.class);
  18. System.out.println(person);
  19. //默认为true
  20. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
  21. person = objectMapper.readValue(personStr, Person.class);
  22. System.out.println(person);
  23. }
  24. }

执行后的结果如下

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. Person person1 = new Person(1, "zxc", new Date());
  15. Person person2 = new Person(2, "ldh", new Date());
  16. List<Person> persons = new ArrayList<>();
  17. persons.add(person1);
  18. persons.add(person2);
  19. //先转换为json字符串
  20. String personStr = objectMapper.writeValueAsString(persons);
  21. //反序列化为List<user> 集合,1需要通过 TypeReference 来具体传递值
  22. List<Person> persons2 = objectMapper.readValue(personStr, new TypeReference<List<Person>>() {});
  23. for(Person person : persons2) {
  24. System.out.println(person);
  25. }
  26. //2,通过 JavaType 来进行处理返回
  27. JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
  28. List<Person> persons3 = objectMapper.readValue(personStr, javaType);
  29. for(Person person : persons3) {
  30. System.out.println(person);
  31. }
  32. }
  33. }

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ObjectMapper 如何忽略字段大小写 https://www.kuaiidc.com/104535.html

相关文章

发表评论
暂无评论