35

I'm new to spring boot and learning @RequestParam()

I know that we can give defaultValue in String but when I am trying to give default value as Integer it's showing me an error.

@RequestMapping("/returnVeriable")
public int getVeriable(@RequestParam(required=true,defaultValue=1/*error here*/) int veri){
    return veri;
}

any help would be appreciated.

2
  • defaultValue is a String specify the default as a String value. That is also what the compiler is telling you. Commented Dec 14, 2017 at 12:58
  • @Siddhesh how about accepting one of the answers? :) Commented Jun 17, 2020 at 7:48

3 Answers 3

27

Try with "" around integer to make it string as the defaultValue is implemented as String.

@RequestMapping("/returnVeriable")
public int getVeriable(@RequestParam(required=true,defaultValue="1") Integer veri){
    return veri;
}

refer : https://jira.spring.io/browse/SPR-5915

Sign up to request clarification or add additional context in comments.

3 Comments

Although required=true together with defaultValue does not make much sense as defaultValue implicitly sets required to false.
Is there a reason you use Integer instead of int?
Integer can be null, but int cannot. If for some reason the param is blank, it would seem that they had put 0 instead of null.
11

When a value traffic by HTTP protocol, it has no type. It's a String. And so it is at this parameter type at the annotation. Then, you do something like this:

@RequestParam(required = true, defaultValue = "1") Integer veri

And it will work fine.

2 Comments

This has nothing to do with HTTP. Were it implemented differently, the Java @RequestParam could take an integer default value.
It could, but it’s not. And it will be a string that was never parsed to int before.
7

This should work

@RequestMapping("/returnVariable")
public int getVariable(@RequestParam(required=true,defaultValue="1") int var) {
    return var;
}

By default what ever you pass to the controller is treated as String and converted to respective types. So even default values need to be set as String

5 Comments

you are converting it into a String.. I want to use it as int.
@Siddhesh defaultValue allways has to be a String and will automatically be converted to the actual type. See: stackoverflow.com/q/12296642/6073886
HTTP Request Parameters are always String... The conversion from String to the actual type is done by Spring using Converters and PropertyEditors.
It is not coverted. See the return type of defaultValue() method in RequestParam class. it is a String. So you need to specify your int as String(with quotes). but the type of variable veri is still int as you can see in the above code
The origin of the annotation parameter type being String instead of Object isn't even with Spring. It comes from a limitation in Java itself: stackoverflow.com/a/1458556/593146

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.