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={“https://stackoverflow.com/”, “/{id}”}

@RequestMapping(method=GET, value={"https://stackoverflow.com/", "/{id}"})
public void get(@PathVariable Optional<Integer> id) {
  if (id.isPresent()) {
    id.get()   //returns the id
  }
}

Leave a Comment