34

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.

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
Siddhesh
  • 1,095
  • 1
  • 11
  • 23

3 Answers3

26

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

Ajay Dsouza
  • 307
  • 3
  • 10
  • 5
    Although `required=true` together with `defaultValue` does not make much sense as `defaultValue` implicitly sets required to false. – GedankenNebel Mar 31 '21 at 20:52
  • Is there a reason you use `Integer` instead of `int`? – Zyl Nov 25 '21 at 20:41
  • 1
    `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. – IcyIcicle Feb 08 '22 at 22:07
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.

Fabiano
  • 1,344
  • 9
  • 24
  • This has nothing to do with HTTP. Were it implemented differently, the Java @RequestParam could take an integer default value. – Heuriskos Aug 22 '22 at 15:39
  • It could, but it’s not. And it will be a string that was never parsed to int before. – Fabiano Aug 23 '22 at 16:08
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

Yevgen
  • 1,576
  • 1
  • 15
  • 17
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • you are converting it into a String.. I want to use it as int. – Siddhesh Dec 14 '17 at 13:00
  • 1
    @Siddhesh defaultValue allways has to be a String and will automatically be converted to the actual type. See: https://stackoverflow.com/q/12296642/6073886 – OH GOD SPIDERS Dec 14 '17 at 13:05
  • 2
    HTTP Request Parameters are always `String`... The conversion from `String` to the actual type is done by Spring using `Converter`s and `PropertyEditor`s. – M. Deinum Dec 14 '17 at 13:07
  • 1
    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 – pvpkiran Dec 14 '17 at 13:07
  • 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: https://stackoverflow.com/a/1458556/593146 – Zyl Nov 25 '21 at 20:43