我已经有了创建POJO对象的基本部分我已经定义了我可以将基本的JSON结构映射到那些对象,但是我需要解析返回不同类型对象的某个API,你永远不知道什么类型它将返回
现在,在API可以返回的所有数据中,我已经能够创建通用的父类,然后是适当的子类,可能允许正确表示API可以分发的任何内容,但是如何告诉映射在这种情况下做的工作?
我正在使用这样的东西:
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TestParent myHeader = mapper.readValue(headerJson, TestParent.class);
System.out.println(myHeader.getWhatAmI());
TestParent是TestHeader的超级版本,我希望映射器可以根据JSON返回的所有JSON最适合TestHeader,然后映射到它,但它确实只使用TestParent并停止那里。我会注意到在这种情况下,当我调用API时,我真的不知道哪种类型的对象确实会被返回。这就是为什么我认为我必须在这里指定TestParent。但是当我运行它时,返回字符串“Parent”。
{ "level": 1,
"proto": "test",
"id": 123 }
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"level",
"proto",
})
public class TestParent implements Serializable
{
private static final long serialVersionUID = 6150510755722173178L;
@JsonProperty("level")
protected Integer level;
@JsonProperty("proto")
protected String proto;
protected String whatAmI;
public TestParent() {
whatAmI = "Parent";
}
@JsonProperty("level")
public Integer getLevel() {
return level;
}
@JsonProperty("proto")
public String getProto() {
return proto;
}
public String getWhatAmI() {
return whatAmI;
}
}
///////////////////////////////////////////
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"priority"
})
public class Child1 extends TestParent implements Serializable
{
private static final long serialVersionUID = 8750271072576382586L;
@JsonProperty("priority")
private String pri;
public Child1() {
super();
whatAmI = "Child1";
}
@JsonProperty("priority")
public String getPriority() {
return pri;
}
}
/////////////////////////////////////////////// ///////
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"priority"
})
public class Child2 extends TestParent implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 8750271072576382586L;
@JsonProperty("id")
private Integer id;
public Child2() {
super();
whatAmI = "Child2";
}
@JsonProperty("id")
public Integer getPriority() {
return id;
}
}
/////////////////////////////////////////////// ///////
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import TestParent;
import Child1;
import Child2;
public class Test {
public static void main(String[] args) throws Exception {
String json = "{ \"level\": 1,\"proto\": \"test\",\"id\": 123}";
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TestParent someObject = mapper.readValue(json, TestParent.class);
System.out.println(someObject.getWhatAmI());
}
}