From 3685d331d9e4c1b7e3ff61d38b3ba4f9c81ed574 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 1 Dec 2019 15:46:00 +0100 Subject: [PATCH] BAEL-3226: Add java.util.Optional @RequestParam example (#8279) --- .../spring/requestparam/RequestParamController.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/requestparam/RequestParamController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/requestparam/RequestParamController.java index bcb1fe5a82..eb445f7801 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/requestparam/RequestParamController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/requestparam/RequestParamController.java @@ -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 id){ + return "ID: " + id.orElseGet(() -> "not provided"); + } @GetMapping("/api/foos3") @ResponseBody