Spring is converting String values to false for @queryparam Boolean

Viewed 289

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

1 Answers

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
  }
}
Related