Is it possible to have empty RequestParam values use the defaultValue?

JavaSpringSpring Mvc

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

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

Solution 4 - Java

You can use @Nullable with default value.

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

Solution 5 - Java

You can also do something like this -

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

Solution 6 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrett RyanView Question on Stackoverflow
Solution 1 - JavaMattView Answer on Stackoverflow
Solution 2 - JavaAppLendView Answer on Stackoverflow
Solution 3 - JavaAnand TagoreView Answer on Stackoverflow
Solution 4 - JavaAbd AbughazalehView Answer on Stackoverflow
Solution 5 - JavaTishaView Answer on Stackoverflow
Solution 6 - JavaeisView Answer on Stackoverflow