How to parse list or String in one variable

I am working on a JSON Serialisation and i have a scenerio where a field in JSON can be a String/ List of Strings. I am not sure how to serialise such object.

@Data
public class NonNullRatio extends Constraint {
    @NonNull private List<String> column;
    @NonNull private Assertion assertion;
}

The input JSON Could be –

"        "constraints": [n" +
    "          {n" +
    "            "type": "NonNullRatio",n" +
    "            "column": ["transaction_status", "user_charge_id", "psp_transaction_id"],n" +
    "            "assertion": {n" +
    "              "type": "LowerLimit",n" +
    "              "threshold": 1n" +
    "            }n" +
    "          },n" +

OR

"        "constraints": [n" +
            "          {n" +
            "            "type": "NonNullRatio",n" +
            "            "column": "ufi",n" +
            "            "assertion": {n" +
            "              "type": "LowerLimit",n" +
            "              "threshold": 1n" +
            "            }n" +
            "          },n" +

Answer

You need to tell your Jackson to accept String as Array.

You can do this using –

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)

Your POJO Should look like –

@Data
public class NonNullRatio extends Constraint {
    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    @NonNull private List<String> column;
    @NonNull private Assertion assertion;
}