Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring, @RequestParameter annotation not accepting boolean for defaultValue argument, it only works with string URL request parameters

This question is related to Spring MVC method I have defined below:

The value for the writeToRead parameter should be true or false. I expect the enduser calling my service to append to the url: localhost:8080/index/someendpoint/sometype the following request parameter: ?writeToRead=true otherwise it should default to false if user doesn't append the parameter to the end of the url string. The problem is, the defaultValue=false doesn't seem to be acceptable in the @RequestParameter annotation. It looks like it only accepts string types rather than the boolean type I'm using. I could make defaultValue="false" but really, that's not a boolean. It's a string. How should I approach this? I can't use string alternatives (like "Y" or "N") to solve this problem because of method overloaded definition conflicts. How can I do this with booleans? Thank you in advance!

@RequestMapping(value="/index/{endpoint}/{type}", method=RequestMethod.POST, consumes=kCompressed, produces=kProducesType)
@ResponseBody
public String indexData(@PathVariable(value="endpoint") String endpoint, @PathVariable(value="type") String type, 
                        @RequestParam(value="writeToRead", defaultValue=false) boolean writeToRead,                 
                        @RequestBody byte[] body, HttpServletRequest request) throws Exception {
    logger.debug("In indexData for endpoint " + endpoint);

    String bodyStr = decompress(body);
    return indexController.indexData(endpoint, type, bodyStr, writeToRead, getSecurityContextProvider(request));
}   
like image 250
Horse Voice Avatar asked Jan 14 '14 15:01

Horse Voice


People also ask

Can request param be Boolean?

The 'required' attribute of @RequestParamIt is Boolean type attribute whether the parameter is required. The default is true. If parameter is missing in the request then it will be returned status code 400. We can override this to false if the parameter is not present in the request.

How do you validate query parameters in spring boot?

Validating a PathVariable Just as with @RequestParam, we can use any annotation from the javax. validation. constraints package to validate a @PathVariable. The default message can be easily overwritten by setting the message parameter in the @Size annotation.

What is required in @RequestParam?

With the @RequestParam annotation, we bind the request parameter to the method variable. The defaultValue option gives a default value if the parameter is not available (the text input was left empty). The required option tells that the parameter is required. The method retuns a string.

Is RequestParam required by default?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.


1 Answers

Declaring it as

@RequestParam(value="writeToRead", defaultValue="false")

is the appropriate thing to do. Request parameters are character strings, nothing else. You can parse them and give them the semantics you want or require (ex. booleans), but, at the base, they are just strings.

like image 183
Sotirios Delimanolis Avatar answered Oct 04 '22 14:10

Sotirios Delimanolis