8
votes

Is it possibile to set a default value to a @PathVariable in SpringMVC?

 @RequestMapping(value = {"/core/organization/{pageNumber}", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap) {

In this case. If I access the page without pageNumber I want to set a default value to 1.

Is that possible?

2
I doubt it, as you can't have default values for method params in javaNimChimpsky
No you cannot have a default for a path variable as without the variable the URL would be different and thus not match. You can always create a mapping for /core/organization which internally calls the list method with the default value you want.M. Deinum

2 Answers

1
votes

There's no way to to set a default value, but you can create two methods:

@RequestMapping(value = {"/core/organization/{pageNumber}", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap){
...
}


@RequestMapping(value = {"/core/organization/", "/core/organization"} , method = RequestMethod.GET)
    public String list(@PathVariable Integer pageNumber, ModelMap modelMap){
Integer pageNumber=defaultvalue;
...
}
0
votes

I'm not sure if this is what you want, but if you want a default showing up in swagger, you can use @ApiImplicitParams/@ApiImplicitParam to annotate the function, with a defaultValue and paramType="path" specified.