If you are using Spring 4.1 and Java 8 you can use java.util.Optional
which is supported in @RequestParam
, @PathVariable
, @RequestHeader
and @MatrixVariable
in Spring MVC -
@RequestMapping(value = {"/json/{type}", "/json" }, method = RequestMethod.GET)
public @ResponseBody TestBean typedTestBean(
@PathVariable Optional<String> type,
@RequestParam("track") String track) {
if (type.isPresent()) {
//type.get() will return type value
//corresponds to path "/json/{type}"
} else {
//corresponds to path "/json"
}
}