From 6ced667bd548586eb31c25ed8e9decdde536677a Mon Sep 17 00:00:00 2001 From: karan Date: Wed, 15 Jul 2020 00:54:16 -0400 Subject: [PATCH 1/3] pathvariable-annotation correct formatting --- .../CustomWebMvcConfigurationSupport.java | 2 +- .../SiteController.java | 2 +- .../PathVariableAnnotationController.java | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) rename spring-mvc-java-2/src/main/java/com/baeldung/{pathvariable => pathvariable.dottruncated}/CustomWebMvcConfigurationSupport.java (92%) rename spring-mvc-java-2/src/main/java/com/baeldung/{pathvariable => pathvariable.dottruncated}/SiteController.java (96%) create mode 100644 spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java similarity index 92% rename from spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java index 12c208c623..24d5a447ab 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/CustomWebMvcConfigurationSupport.java @@ -1,4 +1,4 @@ -package com.baeldung.pathvariable; +package com.baeldung.pathvariable.dottruncated; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java similarity index 96% rename from spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java index 493161b0eb..c9584afdc9 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable.dottruncated/SiteController.java @@ -1,4 +1,4 @@ -package com.baeldung.pathvariable; +package com.baeldung.pathvariable.dottruncated; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java new file mode 100644 index 0000000000..624ba2d105 --- /dev/null +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java @@ -0,0 +1,79 @@ +package com.baeldung.pathvariable; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; +import java.util.Optional; + +@RestController +public class PathVariableAnnotationController { + @GetMapping("/api/employees/{id}") + @ResponseBody + public String getEmployeesById(@PathVariable String id) { + return "ID: " + id; + } + + @GetMapping("/api/employeeswithvariable/{id}") + @ResponseBody + public String getEmployeesByIdWithVariableName(@PathVariable("id") String employeeId) { + return "ID: " + employeeId; + } + + @GetMapping("/api/employees/{id}/{name}") + @ResponseBody + public String getEmployeesByIdAndName(@PathVariable String id, @PathVariable String name) { + return "ID: " + id + ", name: " + name; + } + + @GetMapping("/api/employeeswithmapvariable/{id}/{name}") + @ResponseBody + public String getEmployeesByIdAndNameWithMapVariable(@PathVariable Map pathVarsMap) { + String id = pathVarsMap.get("id"); + String name = pathVarsMap.get("name"); + if (id != null && name != null) { + return "ID: " + id + ", name: " + name; + } else { + return "Missing Parameters"; + } + } + + @GetMapping(value = { "/api/employeeswithrequired", "/api/employeeswithrequired/{id}" }) + @ResponseBody + public String getEmployeesByIdWithRequired(@PathVariable String id) { + return "ID: " + id; + } + + @GetMapping(value = { "/api/employeeswithrequiredfalse", "/api/employeeswithrequiredfalse/{id}" }) + @ResponseBody + public String getEmployeesByIdWithRequiredFalse(@PathVariable(required = false) String id) { + if (id != null) { + return "ID: " + id; + } else { + return "ID missing"; + } + } + + @GetMapping(value = { "/api/employeeswithoptional", "/api/employeeswithoptional/{id}" }) + @ResponseBody + public String getEmployeesByIdWithOptional(@PathVariable Optional id) { + if (id.isPresent()) { + return "ID: " + id.get(); + } else { + return "ID missing"; + } + } + + @GetMapping(value = { "/api/employeeswithmap/{id}", "/api/employeeswithmap" }) + @ResponseBody + public String getEmployeesByIdWithMap(@PathVariable Map pathVarsMap) { + String id = pathVarsMap.get("id"); + if (id != null) { + return "ID: " + id; + } else { + return "ID missing"; + } + } +} From c4770ac40f83ee773426004fd8a4168b57a37aa5 Mon Sep 17 00:00:00 2001 From: karan Date: Fri, 24 Jul 2020 01:08:45 -0400 Subject: [PATCH 2/3] add another defaultpathvariable method --- .../pathvariable/PathVariableAnnotationController.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java index 624ba2d105..37e104f354 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java @@ -66,6 +66,16 @@ public class PathVariableAnnotationController { } } + @GetMapping(value = { "/api/defaultemployeeswithoptional", "/api/defaultemployeeswithoptional/{id}" }) + @ResponseBody + public String getDefaultEmployeesByIdWithOptional(@PathVariable Optional id) { + if (id.isPresent()) { + return "ID: " + id.get(); + } else { + return "ID: Default Employee"; + } + } + @GetMapping(value = { "/api/employeeswithmap/{id}", "/api/employeeswithmap" }) @ResponseBody public String getEmployeesByIdWithMap(@PathVariable Map pathVarsMap) { From d444ab4d54a8aed35cc6be8cc5731df98d7388f7 Mon Sep 17 00:00:00 2001 From: karan Date: Thu, 6 Aug 2020 13:40:44 -0400 Subject: [PATCH 3/3] Fix formatting --- .../PathVariableAnnotationController.java | 136 +++++++++--------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java index 37e104f354..0cbd852095 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/PathVariableAnnotationController.java @@ -10,80 +10,80 @@ import java.util.Optional; @RestController public class PathVariableAnnotationController { - @GetMapping("/api/employees/{id}") - @ResponseBody - public String getEmployeesById(@PathVariable String id) { - return "ID: " + id; - } + @GetMapping("/api/employees/{id}") + @ResponseBody + public String getEmployeesById(@PathVariable String id) { + return "ID: " + id; + } - @GetMapping("/api/employeeswithvariable/{id}") - @ResponseBody - public String getEmployeesByIdWithVariableName(@PathVariable("id") String employeeId) { - return "ID: " + employeeId; - } + @GetMapping("/api/employeeswithvariable/{id}") + @ResponseBody + public String getEmployeesByIdWithVariableName(@PathVariable("id") String employeeId) { + return "ID: " + employeeId; + } - @GetMapping("/api/employees/{id}/{name}") - @ResponseBody - public String getEmployeesByIdAndName(@PathVariable String id, @PathVariable String name) { - return "ID: " + id + ", name: " + name; - } + @GetMapping("/api/employees/{id}/{name}") + @ResponseBody + public String getEmployeesByIdAndName(@PathVariable String id, @PathVariable String name) { + return "ID: " + id + ", name: " + name; + } - @GetMapping("/api/employeeswithmapvariable/{id}/{name}") - @ResponseBody - public String getEmployeesByIdAndNameWithMapVariable(@PathVariable Map pathVarsMap) { - String id = pathVarsMap.get("id"); - String name = pathVarsMap.get("name"); - if (id != null && name != null) { - return "ID: " + id + ", name: " + name; - } else { - return "Missing Parameters"; - } - } + @GetMapping("/api/employeeswithmapvariable/{id}/{name}") + @ResponseBody + public String getEmployeesByIdAndNameWithMapVariable(@PathVariable Map pathVarsMap) { + String id = pathVarsMap.get("id"); + String name = pathVarsMap.get("name"); + if (id != null && name != null) { + return "ID: " + id + ", name: " + name; + } else { + return "Missing Parameters"; + } + } - @GetMapping(value = { "/api/employeeswithrequired", "/api/employeeswithrequired/{id}" }) - @ResponseBody - public String getEmployeesByIdWithRequired(@PathVariable String id) { - return "ID: " + id; - } + @GetMapping(value = { "/api/employeeswithrequired", "/api/employeeswithrequired/{id}" }) + @ResponseBody + public String getEmployeesByIdWithRequired(@PathVariable String id) { + return "ID: " + id; + } - @GetMapping(value = { "/api/employeeswithrequiredfalse", "/api/employeeswithrequiredfalse/{id}" }) - @ResponseBody - public String getEmployeesByIdWithRequiredFalse(@PathVariable(required = false) String id) { - if (id != null) { - return "ID: " + id; - } else { - return "ID missing"; - } - } + @GetMapping(value = { "/api/employeeswithrequiredfalse", "/api/employeeswithrequiredfalse/{id}" }) + @ResponseBody + public String getEmployeesByIdWithRequiredFalse(@PathVariable(required = false) String id) { + if (id != null) { + return "ID: " + id; + } else { + return "ID missing"; + } + } - @GetMapping(value = { "/api/employeeswithoptional", "/api/employeeswithoptional/{id}" }) - @ResponseBody - public String getEmployeesByIdWithOptional(@PathVariable Optional id) { - if (id.isPresent()) { - return "ID: " + id.get(); - } else { - return "ID missing"; - } - } + @GetMapping(value = { "/api/employeeswithoptional", "/api/employeeswithoptional/{id}" }) + @ResponseBody + public String getEmployeesByIdWithOptional(@PathVariable Optional id) { + if (id.isPresent()) { + return "ID: " + id.get(); + } else { + return "ID missing"; + } + } - @GetMapping(value = { "/api/defaultemployeeswithoptional", "/api/defaultemployeeswithoptional/{id}" }) - @ResponseBody - public String getDefaultEmployeesByIdWithOptional(@PathVariable Optional id) { - if (id.isPresent()) { - return "ID: " + id.get(); - } else { - return "ID: Default Employee"; - } - } + @GetMapping(value = { "/api/defaultemployeeswithoptional", "/api/defaultemployeeswithoptional/{id}" }) + @ResponseBody + public String getDefaultEmployeesByIdWithOptional(@PathVariable Optional id) { + if (id.isPresent()) { + return "ID: " + id.get(); + } else { + return "ID: Default Employee"; + } + } - @GetMapping(value = { "/api/employeeswithmap/{id}", "/api/employeeswithmap" }) - @ResponseBody - public String getEmployeesByIdWithMap(@PathVariable Map pathVarsMap) { - String id = pathVarsMap.get("id"); - if (id != null) { - return "ID: " + id; - } else { - return "ID missing"; - } - } + @GetMapping(value = { "/api/employeeswithmap/{id}", "/api/employeeswithmap" }) + @ResponseBody + public String getEmployeesByIdWithMap(@PathVariable Map pathVarsMap) { + String id = pathVarsMap.get("id"); + if (id != null) { + return "ID: " + id; + } else { + return "ID missing"; + } + } }