一般能夠經過註解解決,@JsonTypeInfo,@JsonSubTypes解決。app
還能夠經過泛型解決,利用JavaType的轉換機制。get
如:class
calss A {泛型
B b;map
}co
calss B{註解
String id;struct
}const
calss C extend B{
String name;
}
calss D extend B{
String address;
}
JavaType bc=mapper.getTypeFactory().constructParametricType(B.class, C.class);
JavaType bd=mapper.getTypeFactory().constructParametricType(B.class, D.class);
b = mapper.readValue(obj, bc);
b = mapper.readValue(obj, bd);