167

if I have a a request mapping similar to the following:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", defaultValue = "10") int i) {
}

And then call this request with:

http://example.com/test?i=

I get the error message

Failed to convert value of type 'java.lang.String' to type 'int'; nested exception is java.lang.NumberFormatException: For input string: ""'

I can solve this by either stopping the javascript client from sending empty parameters, or by accepting string values and only parsing if they are not found to be blank.

UPDATE: Later versions of spring now implement the originally desired behaviour.

I've just tested this in spring 4.3.5 and have found that the behaviour will now in fact turn the null value into the default value without raising a NumberFormatException, thus; my original mapping now works fine.

I am not sure which version of spring this behavioural change was made.

3
  • surely the parameter would be an Integer type to raise that exception? Commented Sep 6, 2012 at 9:13
  • 4
    im not spring expert, but is default value is used when you don't set it? ie if your request will be example.com/test ? as you use example.com/test?i= then i is present and set to "" so default value is not used Commented Sep 6, 2012 at 9:40
  • Thanks amir75, accidentally supplied String type. user902383, that's correct, though for an integer value I would prefer that a null/empty value would use the defaultValue instead. Commented Sep 6, 2012 at 9:46

6 Answers 6

226

You could change the @RequestParam type to an Integer and make it not required. This would allow your request to succeed, but it would then be null. You could explicitly set it to your default value in the controller method:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", required=false) Integer i) {
    if(i == null) {
        i = 10;
    }
    // ...
}

I have removed the defaultValue from the example above, but you may want to include it if you expect to receive requests where it isn't set at all:

http://example.com/test
Sign up to request clarification or add additional context in comments.

5 Comments

Just an update on this, recent versions of spring actually no longer require the null check as the desired behaviour has been included with spring that empty values will result in the default value being used.
Obsolete answers, the best solution is from @AppLend
In modern Spring versions, it's called defaultValue, and value is just an alias to name.
I tried this, but still get the same error: @RequestParam(required = false) Boolean password and then if (password != null && password). My Spring Boot version is 2.6.2
Does it return an empty String or something if there's no parameter? What's the workaround?
131

You can keep primitive type by setting default value, in the your case just add "required = false" property:

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

P.S. This page from Spring documentation might be useful: Annotation Type RequestParam

4 Comments

Thanks but you missed the point of my question which is in the title. I've asked for the ability to supply empty params /test?i= and since i is empty have the default value.
Anyway, when defaultValue was provided, required == false implicitly
If the param is provided but is empty defaultValue does not take effect.
Still does not work at least for @GetMapping request. The defaultValue is not set (null for Integer, empty string for String, fails with exception for int. :(
20

You can set RequestParam, using generic class Integer instead of int, it will resolve your issue.

   @RequestParam(value= "i", defaultValue = "20") Integer i

Comments

5

You can use @Nullable with default value.

@Nullable @RequestParam(value = "i", defaultValue = "10") Integer i

Comments

2

You can also do something like this -

 @RequestParam(value= "i", defaultValue = "20") Optional<Integer> i

1 Comment

If you have a defaultValue, there's no need to make this an Optional.
2

This was considered a bug in 2013: https://jira.spring.io/browse/SPR-10180

and was fixed with version 3.2.2. Problem shouldn't occur in any versions after that and your code should work just fine.

Comments

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.