Java Jackson将普通Json数组或Json对象反序列化为单个Java对象(POJO)

Java Jackson将普通Json数组或Json对象反序列化为单个Java对象(POJO),java,json,jackson,deserialization,jackson2,Java,Json,Jackson,Deserialization,Jackson2,我正在从API端点检索数据,该端点以两种不同的预期json格式返回数据。其中一个响应是成功响应,另一个响应是失败响应。API中的其他端点类似,也有成功和失败的格式。对于大多数端点,我只是将这两种类型的响应反序列化为单个Java对象,其中一些字段在不存在时仅设置为默认值/空值。 这个端点的问题是成功响应是一个json数组,而失败响应是一个json对象。我尽了一切努力,想用一种简单的方法将这两种可能的响应反序列化为一个java对象 示例成功json [ { "item1":

我正在从API端点检索数据,该端点以两种不同的预期json格式返回数据。其中一个响应是成功响应,另一个响应是失败响应。API中的其他端点类似,也有成功和失败的格式。对于大多数端点,我只是将这两种类型的响应反序列化为单个Java对象,其中一些字段在不存在时仅设置为默认值/空值。 这个端点的问题是成功响应是一个json数组,而失败响应是一个json对象。我尽了一切努力,想用一种简单的方法将这两种可能的响应反序列化为一个java对象

示例成功json

[
    {
        "item1": "somevalue1",
        "item2": "somevalue2"
    },
    {
        "item1": "somevalue3",
        "item2": "somevalue4"
    },
    ..
    ..
]
示例失败json

{
    "success": false,
    "errorMessage": "something went wrong "
}
我当前使用的Java类works反序列化成功的json,但不反序列化失败的json

public class ResponseObject {
    public final boolean success;
    public final String errorMessage;
    public final List<MyItem> items;

    @JsonCreator
    public ResponseObject(ArrayList<MyItem> items) {
       this.items = items;
    }

}
无论我尝试了什么策略来将这两个案例反序列化,结果都失败了。如果我试图在带有@JsonProperty的构造函数中包含json数组,那么我没有名称/值来引用项数组,代码会抛出一个异常

我需要的是一种将两个响应反序列化为我概述的ResponseObject格式的方法,当我获得成功响应时,成功字段应为true,errorMessage应为null,项应包含MyItem的列表。 当我得到一个失败响应时,成功应该是false,错误消息应该有一个字符串消息,项目列表应该是null

我怎样才能做到这一点? 或者,我如何构造代码以处理多种预期的json格式? 例如,我知道我可以反序列化到树映射,检查格式,然后再次转换为最终对象,但我想跳过这中间步骤


感谢任何人在这方面给我的建议:)

你可以尝试创建一个超类

public class ResponseObject {

    public final boolean success;
    public final String errorMessage;
    public final List<MyItem> items;

    ...
}

使用相应的构造函数。属性将位于超类中。
当使用这些类时,您应该将它们称为类
ResponseObject

如果为错误消息添加POJO,则可以向响应对象添加第二个JsonCreator

public class ErrorResponse {
    private boolean success;
    private String errorMessage;
    public boolean isSuccess() {
        return success;
    }
    public void setSuccess(boolean success) {
        this.success = success;
    }
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
使用2 JsonCreator修改ResponseObject

public class ResponseObject {
    public final boolean success;
    public final String errorMessage;
    public final List<MyItem> items;

    @JsonCreator
    public ResponseObject(List<MyItem> items) {
       this.success = true;
       this.errorMessage = null;
       this.items = items;
    }

    @JsonCreator
    public ResponseObject(ErrorResponse error){
        this.success = error.isSuccess();
        this.errorMessage = error.getErrorMessage();
        this.items = null;
    }

}
公共类响应对象{
公众的成功;
公共最终字符串错误消息;
公开最后清单项目;
@JsonCreator
公共响应对象(列表项){
这就是成功;
this.errorMessage=null;
这个项目=项目;
}
@JsonCreator
公共响应对象(错误响应错误){
this.success=error.issucess();
this.errorMessage=error.getErrorMessage();
this.items=null;
}
}

这绝对解决了我的问题和原始问题。:)它起作用了。
public class SuccesResponseObject extends ResponseObject {...}
public class FailResponseObject extends ResponseObject {...}
public class ErrorResponse {
    private boolean success;
    private String errorMessage;
    public boolean isSuccess() {
        return success;
    }
    public void setSuccess(boolean success) {
        this.success = success;
    }
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
public class ResponseObject {
    public final boolean success;
    public final String errorMessage;
    public final List<MyItem> items;

    @JsonCreator
    public ResponseObject(List<MyItem> items) {
       this.success = true;
       this.errorMessage = null;
       this.items = items;
    }

    @JsonCreator
    public ResponseObject(ErrorResponse error){
        this.success = error.isSuccess();
        this.errorMessage = error.getErrorMessage();
        this.items = null;
    }

}