ObjectMapper不映射Object的内部Object值

问题描述 投票:0回答:1

这样有一个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之后,BookUser对象是null,而LinkedHashMap result具有书籍对象价值。

我该如何解决我的问题?

编辑:有必要提一下Book对象也有自己的Object,我也要映射Book of Object。

LinkedHashMap值是here

java java-8 objectmapper modelmapper
1个回答
0
投票

我通过从ObjectMapper更改为ModelMapper来解决问题,如:

ModelMapper.map(result, User.class);
© www.soinside.com 2019 - 2024. All rights reserved.