在工作中遇到一个场景:从某数据库中拿到一批数据,拿到的形式是 List<Map<String, Object>>,现有对应的自定义对象,假设为 User.class,但 Map 中的参数与 User 的属性并不是一一对应,且对某些特殊的字段要进行特殊处理。在此情况下,如何实现从 Map 到 User 对象的转换。
正好趁这个机会,总结一下之前用到过的几种方法。
一. 定义 User 对象与 Map
java
@Data
public class User {
private String _key;
private String _id;
private String _rev;
private String name;
private String surname;
private Boolean alive;
private Integer age;
private List<String> traits;
@JsonIgnore
private String test;
}
@JsonIgnore 注解的作用是在 json 序列化时将被注解的属性忽略掉
java
private static Map<String, Object> getMap() {
Map<String, Object> map = new HashMap<>();
map.put("_key", "qwertyui");
map.put("_id", "ttt");
map.put("_rev", "this is a test");
map.put("name", "lizhou");
map.put("surname", null);
map.put("alive", true);
map.put("age", 12);
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
map.put("traits", list);
map.put("test", "test");
return map;
}
二. 五种转化方式
java
private static void useBeanMap(Map<String, Object> map) {
User user = new User();
BeanMap beanMap = BeanMap.create(user);
beanMap.putAll(map);
System.out.println(user);
}
private static void useObjectMapper(Map<String, Object> map) {
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.convertValue(map, User.class);
System.out.println(user);
}
//将map转化为json字符串,然后转为对象
private static void useFastJson(Map<String, Object> map) {
String s = JSON.toJSONString(map);
User user = JSON.parseObject(s, User.class);
System.out.println(user);
}
private static void useGson(Map<String, Object> map) {
Gson gson = new Gson();
String s = gson.toJson(map);
User user = gson.fromJson(s, User.class);
System.out.println(user);
}
private static void useField(Map<String, Object> map) {
try {
User user = new User();
Field[] fields = User.class.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(user, map.get(field.getName()));
}
System.out.println(user);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
1. 调用以上五种方法,查看结果
可以发现,都可以正常将 Map 转化为 User 对象,但是只有 ObjectMapper 方法会受到 @JsonIgnore 注解影响
2. 删除 User 类部分属性
将 User 类中的 test 属性注释掉
java
@Data
public class User {
private String _key;
private String _id;
private String _rev;
private String name;
private String surname;
private Boolean alive;
private Integer age;
private List<String> traits;
// @JsonIgnore
// private String test;
}
再次调用方法,查看结果
可以发现,除了 ObjectMapper 方法以外,其他方法都可以正常运行,ObjectMapper 方法会报错:
java
Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized field "test" (class com.example.demo.bean.User), not marked as ignorable (8 known properties: "_key", "_rev", "traits", "_id", "name", "alive", "age", "surname"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.example.demo.bean.User["test"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3922)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3853)
at com.example.demo.test.Test16.useObjectMapper(Test16.java:35)
at com.example.demo.test.Test16.main(Test16.java:23)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "test" (class com.example.demo.bean.User), not marked as ignorable (8 known properties: "_key", "_rev", "traits", "_id", "name", "alive", "age", "surname"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.example.demo.bean.User["test"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:840)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1206)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1592)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1570)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3917)
... 3 more
所以 ObjectMapper 方法需要 Map 和转化对象的字段完全对应。