Spring optional int parameter

I have a method in a Spring controller, for example:

@RequestMapping(value = "/contact", method = RequestMethod.GET) public final ModelAndView contact(@RequestParam(value = "id", required = false) int i) {..} 

I pass the parameter I to another method. In this method, I want to check if there is a value in int i. If the parameter has not been set (this is optional), I want to set it to a specific value. How to check this in Spring?

+7
java spring
source share
1 answer

Use Integer instead of int . If it is not installed, it will be zero.

If, for example, 0 is not a valid ID value, you can also leave it as an int and use

 @RequestParam(value = "id", defaultValue = "0") 

The parameter will be 0 if it did not pass.

+12
source share

All Articles