anurag1007
anurag1007

Reputation: 137

Spring is converting String values to false for @queryparam Boolean

I have this as queryparam in my requestmapping: @QueryParam("isBenchmark") boolean isBenchmark

Now if am passing any String value also it is defaulting it to false. How can I avoid this and throw error if it is anything other than true/false. Is there any annotations with which I can throw such validations

Upvotes: 1

Views: 1073

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 19108

It is a known issue.

It is converted to boolean using the new Boolean(isBenchmark) So when isBenchmark is true it converts to true, in all other cases it converts to false.

Unfortunately you have to handle it by your self

 public void YourMethod(@QueryParam("isBenchmark") String isBenchmark) {
 boolean val
 if (isBenchmark != null && isBenchmark.equals("true")){
   val = true;
 } else if (isBenchmark != null && isBenchmark.equals("false")) {
   val = false;
 } else {
   Throw Exception or return a bad request
  }
}

Upvotes: 1

Related Questions