java如何在没有类型id的情况下使用Jackson反序列化JSON子类型
我使用的是一个外部服务,它以以下结构发送响应
如果成功:
{
"executionTime": ""
"result": <subclass of ObjectBase which has type id>
}
如果出现故障:
{
"executionTime": ""
"result": {
"error": <ErrorResult which is subtype of ObjectBase>
}
}
如果失败,结果对象中没有直接的类型id,Jackson反序列化失败
我尝试使用defaultImpl,但最终出现了这个错误
原因:java。lang.IllegalArgumentException:Class ErrorResponse不是[simple type,Class ObjectBase]的子类型
public class Response<T> {
private APIException error;
private T result;
public Response(T result, APIException error) {
this.error = error;
this.result = result;
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, visible = true, property = ParamKeys.OBJECT_TYPE, defaultImpl = ErrorResponse.class)
@JsonSubTypes({
@JsonSubTypes.Type(name = ObjectTypes.VALUE, value = Value.class),
})
public class ObjectBase implements Serializable {
}
我们怎样才能在没有任何黑客攻击的情况下以一种干净的方式将其反序列化
提前谢谢
共 (0) 个答案