Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 413
Brett Ryan Avatar asked Sep 06 '12 09:09

Brett Ryan


People also ask

Can RequestParam be null?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

How do I set default value in RequestParam?

By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).

What is required in @RequestParam?

The 'required' attribute of @RequestParam It is Boolean type attribute whether the parameter is required. The default is true. If parameter is missing in the request then it will be returned status code 400. We can override this to false if the parameter is not present in the request.

What is value in @RequestParam?

Spring @RequestParam annotation is used to fetch the value of a parameter in the form request. In Spring MVC, "request parameters" map to query parameters, form data. For example, if we want to get parameter(user_name) value from a requested URL then we can use @RequestParam annotation.


2 Answers

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 
like image 153
Matt Avatar answered Oct 05 '22 21:10

Matt


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

like image 30
AppLend Avatar answered Oct 05 '22 21:10

AppLend