Spring MVC: how to indicate whether a path variable is required or not?
VTTom`s solution is right, just change "value" variable to array and list all url possibilities: value={"/", "/{id}"}
@RequestMapping(method=GET, value={"/", "/{id}"})
public void get(@PathVariable Optional<Integer> id) {
if (id.isPresent()) {
id.get() //returns the id
}
}
There's no way to make it optional, but you can create two methods with one having the @RequestMapping({"customer"})
annotation and the other having @RequestMapping({"customer/{id}"})
and then act accordingly in each.