假设我有以下 Java 类:
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Demo {
public int x;
public int y;
public List<Pair<Integer, Integer>> the_list;
}
我想从例如以下 json 格式填充它:
{ "x" : 1,
"y" : 2,
"the_list" : [[1,2],[3,4]]}
使用更快的xml
ObjectMapper mapper = new ObjectMapper();
我可能可以调用 mapper.readTree(json) 并填写我需要的所有内容。问题是我拥有的实际类(不是 Demo)包含很多参数,我想从数据绑定功能中受益。
尝试一个简单的:
mapper.readValue(json, Demo.class)
给出以下错误:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of
org.apache.commons.lang3.tuple.Pair, problem: abstract types either need to be mapped to
concrete types, have custom deserializer, or be instantiated with additional type
information
有没有办法将自定义解析与数据绑定混合在一起?我查看了注释,但没有找到任何适合目的的东西(我无法让 mixins 与泛型一起使用,未调用 the_list 的自定义设置器可能是因为它是一个列表,JsonCreator 不是一个选项,因为我没有写对类...)。