BAEL-3226: Add java.util.Optional @RequestParam example (#8279)
This commit is contained in:
parent
b83316d625
commit
3685d331d9
|
@ -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;
|
||||
|
@ -31,6 +32,12 @@ public class RequestParamController {
|
|||
public String getFoos2(@RequestParam(required = false) String id){
|
||||
return "ID: " + id;
|
||||
}
|
||||
|
||||
@GetMapping("/api/foosOptional")
|
||||
@ResponseBody
|
||||
public String getFoosOptional(@RequestParam Optional<String> id){
|
||||
return "ID: " + id.orElseGet(() -> "not provided");
|
||||
}
|
||||
|
||||
@GetMapping("/api/foos3")
|
||||
@ResponseBody
|
||||
|
|
Loading…
Reference in New Issue