• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ObjectMapper 如何忽略字段大小写

java 搞代码 4年前 (2022-01-09) 47次浏览 已收录 0个评论
文章目录[隐藏]

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<p>本文来源gao!daima.com搞$代!码#网#</p>;
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);
 }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ObjectMapper 如何忽略字段大小写
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址