Mithun Debnath
Mithun Debnath

Reputation: 588

how to check int and boolean optional parameter in Spring Mvc framework in java

I am working on spring mvc framework and trying to develop a REST Api.

@RequestMapping(value = "/myUrl", method = RequestMethod.POST, produces = StaticUtils.APPLICATION_JSON)
public @ResponseBody String transliterateSimpleJSON(@RequestBody String inStringJson, @RequestParam(value = "target_lang", required = true) String tLang, @RequestParam(value = "check", required = false) boolean check,
        @RequestParam(value = "source_lang", required = false) String srcLang, @RequestParam(value = "domain") int domain, HttpServletRequest request, HttpServletResponse response, Locale locale,
        Model model) {
//My code goes here
return "somthing";
}

Now as you can see srcLang,check and domain are optional parameter.for srcLang I am using the following code :

if (srcLang == null || srcLang.isEmpty()) {
                srcLang = tLang;
            }

But for the parameters like check (which is Boolean) and domain (which is int),I want to set some default value if these are not given in the URL.I want to set check=true and and domain=3.But I have not been able to do that as I did for String srcLang as I don't know how to check int and Boolean value Url parameters inside Controller.

Upvotes: 1

Views: 4984

Answers (1)

Erik Pragt
Erik Pragt

Reputation: 14657

You should use the RequestParam annotation. It has an attribute called defaultValue (type String) which can be used in the case an attribute is not there, like this:

@RequestParam(value="domain", required=false, defaultValue="10") int domain)

Upvotes: 2

Related Questions