ObjectMapper不映射对象的内部Object值

时间:2019-04-17 07:14:22

标签: java java-8 objectmapper modelmapper

有一个User对象,像这样:

public class User {
    private String name;
    private String family;
    private Book book;
    // With its own setter & getter methods
}

以下代码用于将LinkedHashMap值转换为User对象:

LinkedHashMap result = linkedHashMap.get("principal");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User user = objectMapper.convertValue(result, User.class);

但是在objectMapper.convertValue之后,Book的{​​{1}}对象是User,而null具有书本对象值。

如何解决我的问题?

修改: 有必要提到LinkedHashMap result对象也有它自己的对象,我也想映射Book的对象。

Book的值为here

1 个答案:

答案 0 :(得分:0)

我通过将ObjectMapper更改为ModelMapper来解决该问题,

ModelMapper.map(result, User.class);