Support Both Object and Array in Json Otherwise it throws MismatchedInputException

21 views Asked by At

I use a third-party service and it behaves interesting.

it returns object for empty list:

{
    "list": {},
    "total": 0
}

Otherwise, it returns list

{
    "list": [
        {
            "createdIndex": 118,
            "key": "/apisix/services/00000000000000000117",
            "modifiedIndex": 119
        }
    ],
    "total": 1
}

When I try to deserialize it on Java, if it is not empty I got MismatchedInputException.

Because my java class looks like:

@Getter
@Setter
public class Response<T> {

    @JsonProperty("error_msg")
    private String errorMsg;
    private String key;
    private T value;

    private List<Response<T>> list;
    private int total;
}

Is there any suggestion?

0

There are 0 answers