JsonProperty in JAVA - defining two variables using the same property

Viewed 85

I am having a class like,

 public class Test {

    @JsonProperty("values")
    private List<String> valuesString = null;

    @JsonProperty("values")
    private List<Boolean> valuesBool = null;
}

if I give a list of string like below then it needs to be stored in valuesString, body,

 {
   "values" : ["Test1", "Test2"]
 }

if list of boolean then valuesBool,

   {
       "values" : [false, true]
    }

As of now it is giving me 400 response.

1 Answers

This is fixing my issue,

when we are passing boolean it's converted to String in this case,

 public class Test {
    @JsonProperty("values")
    private List<String> valuesString = null;
}

request body,

    {
       "values" : [false, true]
    }

And if you want to get as a boolean in implementation use below,

Boolean.parseBoolean("true");
Related