Spring @RequestParam mapping Boolean based on 1 or 0 instead of true or false
Asked Answered
L

5

53

Why is Spring 3.2 only mapping my Boolean based on that the requestparam is "0" or "1" ?

@RequestParam(required= false, defaultValue = "false") Boolean preview

Preview will only be "true" when the requestparam is "?preview=1" which is wierd

I want it to be "?preview=true". How do I do that?

Lightfooted answered 4/10, 2013 at 10:18 Comment(1)
did you know a way, where the request parameter which has default value and required is false, that can be totally ignored in the URL, meaning we don't even have to put anything in the URL for that request parameter. In your example, preview will not even be a part of the URL.Backbone
S
114

I think we may need more detail in order to be effective answering your question.

I have working Spring 3.2 code along the lines of:

@RequestMapping(value = "/foo/{id}", method = RequestMethod.GET)
@ResponseBody
public Foo getFoo(
    @PathVariable("id") String id, 
    @RequestParam(value="bar", required = false, defaultValue = "true")
        boolean bar)
{ 
    ... 
}

Spring correctly interprets ?bar=true, ?bar=1, or ?bar=yes as being true, and ?bar=false, ?bar=0, or ?bar=no as being false.

True/false and yes/no values ignore case.

Sauerbraten answered 4/8, 2014 at 16:9 Comment(3)
Does it do equalsIgnoreCase as well? I mean what would be expected result if I send bar=TRUE ,bar=TrUe or bar=yES...?Xenia
According to the sorucecode linked by Pavel below, it ignores case.Preliminaries
Just a small info: Supplying a defaultValue implicitly sets required as false. So don't need to pass required=false.Rework
B
25

Spring should be able to interpret true, 1, yes and on as true boolean value... check StringToBooleanConverter.

Bone answered 4/10, 2013 at 18:45 Comment(3)
is there a way in which, we turn required off and provide a default value and in the URL we don't even have to mention the request parameter?Backbone
{{@RequestParam(defaultValue = "true") boolean notify}} Should suffice to solve your problem.Montero
@SoumitriPattnaik Supplying a default value implicitly sets required to false.Lyudmila
U
1

I suppose you are missing value="preview" in the @RequestParam. Due to this, Spring is setting the variable preview to false always, as that is the defaultValue.

@RequestParam(value = "preview", required= false, defaultValue = "false") Boolean preview
Unthread answered 19/12, 2022 at 15:19 Comment(0)
W
0

you can use jackson's JsonDeserialize annotation, short and clean

create the following deserializer:

public class BooleanDeserializer extends JsonDeserializer<Boolean> {
    public BooleanDeserializer() {
    }

    public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        return !"0".equals(parser.getText());
    }
}

and add the annotation to your DTO like so:

public class MyDTO {
    @NotNull
    @JsonDeserialize(using = BooleanDeserializer.class)
    private Boolean confirmation;
}
Wakefield answered 28/5, 2020 at 11:28 Comment(0)
M
0

We can do using

@GetMapping("/getAudioDetails/organizations/{orgId}/interactions/{interactionId}")
public ResponseEntity<?> getAudioDetails(@PathVariable("orgId") String orgId,@PathVariable("interactionId") String interactionId,
        @RequestParam(name = "verbose", required = false, defaultValue = "false") boolean verbose){
    System.out.println(verbose);
    return null;
}

where verbose is an boolean flag which by default happens to be false as can be referred from inage below on api call .enter image description here

Memberg answered 30/3, 2021 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.