8

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 java Commented Feb 1, 2015 at 18:57
  • 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. Commented Feb 1, 2015 at 20:16

2 Answers 2

1

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;
...
}

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.