我想用杰克逊来绑
Map<String, String>
为了一粒豆子。这里的陷阱是,并非所有字段都是集合,因此无法工作。
当对应的bean属性是集合时,我需要调整objectmapper以仅绑定集合。
public class MapperTest {
public static class Person {
public String fname;
public Double age;
public List<String> other;
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Map<String, String[]> props = new HashMap<>();
props.put("fname", new String[] {"mridang"});
props.put("age", new String[] {"1"});
props.put("other", new String[] {"one", "two"});
mapper.convertValue(props, Person.class);
}
}
上面的示例不起作用,因为Jackson希望所有字段都是集合。
我不能改变地图结构,因为这是一个我正在处理的遗留系统,所以我几乎被卡住了。
Map<String, String[]>