Add java.util.Optional example
@@ -2,6 +2,7 @@ package com.baeldung.spring.requestparam;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@@ -32,6 +33,12 @@ public class RequestParamController {
return "ID: " + id;
}
+ @GetMapping("/api/foosOptional")
+ @ResponseBody
+ public String getFoosOptional(@RequestParam Optional<String> id){
+ return "ID: " + id.orElseGet(() -> "not provided");
+ }
+
@GetMapping("/api/foos3")
@ResponseBody
public String getFoos3(@RequestParam(defaultValue = "test") String id){
