似乎无法弄清楚。我不断收到各种错误,所以我只用从杰克逊得到的当前错误来写。
public class ResponseDetail {
private Response response;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = ResponseTypeOne.class, name = "ResponseTypeOne"),
@JsonSubTypes.Type(value = ResponseTypeTwo.class, name = "ResponseTypeTwo"),
@JsonSubTypes.Type(value = ResponseTypeThree.class, name = "ResponseTypeThree")
})
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Response {
}
在其他软件包中,我具有以下三个:
public class ResponseTypeOne extends Response {
private Integer status;
}
public class ResponseTypeTwo extends Response {
private String message;
}
public class ResponseTypeThree extends Response {
private String value;
}
错误:
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class com.services.models.Response]: missing type id property '@type' (for POJO property 'response')
我已经试过这各种迭代@JsonTypeInfo
各种includes
各种property
的也Id.CLASS
没有运气。
您需要声明如何识别类型。
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "@ttype")
@JsonSubTypes({
@JsonSubTypes.Type(value = ResponseTypeOne.class, name = "ResponseTypeOne"),
@JsonSubTypes.Type(value = ResponseTypeTwo.class, name = "ResponseTypeTwo"),
@JsonSubTypes.Type(value = ResponseTypeThree.class, name = "ResponseTypeThree")
})
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Response {
@JsonProperty("@ttype")
public abstract String getChildType();
}
在子类型中,请执行以下操作:
@JsonTypeName("ResponseTypeOne")
public class ResponseTypeOne extends Response{
@Override
public String getChildType(){
return "ResponseTypeOne";
}
}
传入的json应该如下所示,以使jackson能够找到正确的子实现:
{
//some attributes of child Response
"@ttype": "ResponseTypeOne"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句