BAEL-3226: Add java.util.Optional @RequestParam example (#8279)

This commit is contained in:
kwoyke 2019-12-01 15:46:00 +01:00 committed by Grzegorz Piwowarek
parent b83316d625
commit 3685d331d9
1 changed files with 7 additions and 0 deletions

View File

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