ObjectMapper 忽略字段大小写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42713970/article/details/88061100

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 + "]";
		}

		
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42713970/article/details/88061100