In Spring, @RequestParameter annotation not accepting boolean for defaultValue argument, it only works with string URL request parameters
java, spring, spring-mvc
Solution
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.
Problem
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)); } ```