Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we give @QueryParam of type boolean a default value null?

Tags:

java

jax-rs

can we give @QueryParam of type boolean a default value null ? if yes, how do we do it?

this is how I tried to do it but I still get false as default value:

@POST
public String setMethod(
        @QueryParam("value1") @DefaultValue("null") Boolean value1)
like image 827
Ohad Avatar asked Oct 16 '25 10:10

Ohad


1 Answers

I am facing the same problem, and unfortunately there isn't a direct way to solve this.

Sadly, we can't use enums because @RequestParam defaultValue and switch-case need a constant. It also can't automatically map it from "null" into the enum.

@POST
public String setMethod(
    @QueryParam("value1") @DefaultValue("null") String value1)


Boolean boolValue1 = null;    
switch(value1) {

    case "true":
        boolValue1 = Boolean.TRUE;
        break;

    case "false":
        boolValue1 = Boolean.FALSE;
        break;

    default:
        break;
}

Since there isn't a case for null, it should remain null.

like image 146
Marco Frag Delle Monache Avatar answered Oct 18 '25 04:10

Marco Frag Delle Monache



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!