@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = As.PROPERTY,
property = "type")
@JsonSubTypes(value = {
@Type(value = GeoEntity.class, name = "geo"),
@Type(value = FioEntity.class, name = "fio"),
@Type(value = Number.class, name ="number")
})
public abstract class Entity {
public void setType(String type) {
this.type = type;
}
private String type;
@Override
public String toString(){
return ToStringBuilder.reflectionToString(this);
}
}
FioEntity类(类似于创建GeoEntity的方法)
public class FioEntity extends Entity {
public FioEntityValue getValue() {
return value;
}
public void setValue(FioEntityValue value) {
this.value = value;
}
private FioEntityValue value;
public class FioEntityValue extends EntityValue{
private String firstName;
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
}
编号类别:
public class Number extends Entity {
private Double value;
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
}
测试等级:
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String content = "{\n"
+ " \"entities\": [\n"
+ " {\n"
+ " \"type\": \"fio\",\n"
+ " \"value\": {\n"
+ " \"firstName\": \"first\",\n"
+ " \"lastName\": \"last\"\n"
+ " }\n"
+ " },\n"
+ "\n"
+ " {\n"
+ " \"type\": \"geo\",\n"
+ " \"value\": {\n"
+ " \"country\": \"the country\",\n"
+ " \"city\": \"the city\"\n"
+ " }\n"
+ " },\n"
+ "\n"
+ " {\n"
+ " \"type\": \"number\",\n"
+ " \"value\": 100\n"
+ " },\n"
+ "\n"
+ " {\n"
+ " \"type\": \"number\",\n"
+ " \"value\": 3.14\n"
+ " }\n"
+ " ]\n"
+ "}";
Entities test = mapper.readValue(content, Entities.class);
System.out.println(mapper.writeValueAsString(test));
}
}
输出:
{"entities":[{"type":"fio","value":{"firstName":"first","lastName":"last"}},{"type":"geo","value":{"city":"the city","country":"the country"}},{"type":"number","value":100.0},{"type":"number","value":3.14}]}