From 0f71d7d2b8ea7af8e30ca664cd70ee5300f74d5e Mon Sep 17 00:00:00 2001 From: Eugen Date: Thu, 16 May 2013 17:33:00 +0300 Subject: [PATCH] minor mapping work --- .../spring/web/controller/BarController.java | 9 +++++++ .../spring/web/controller/FooController.java | 26 ++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java b/spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java index def8ffd11e..4c2202791c 100644 --- a/spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java +++ b/spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java @@ -1,6 +1,7 @@ package org.baeldung.spring.web.controller; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @@ -34,4 +35,12 @@ public class BarController { return "Get a specific Bar with id=" + id; } + // with @PathVariable + + @RequestMapping(value = "/bars/{numericId:[\\d]+}") + @ResponseBody + public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { + return "Get a specific Bar with id=" + numericId; + } + } diff --git a/spring-mvc/src/main/java/org/baeldung/spring/web/controller/FooController.java b/spring-mvc/src/main/java/org/baeldung/spring/web/controller/FooController.java index 2f86327fc8..32019a4571 100644 --- a/spring-mvc/src/main/java/org/baeldung/spring/web/controller/FooController.java +++ b/spring-mvc/src/main/java/org/baeldung/spring/web/controller/FooController.java @@ -23,20 +23,20 @@ public class FooController { return "Simple Get some Foos"; } - // @RequestMapping(value = "/foos") - // @ResponseBody - // public String getFoosByAdvancedPath() { - // return "Advanced Get some Foos"; - // } - // with @PathVariable @RequestMapping(value = "/foos/{id}") @ResponseBody - public String getFoosBySimplePathWithPathVariable(@PathVariable("id") final long id) { + public String getFoosBySimplePathWithPathVariable(@PathVariable final long id) { return "Get a specific Foo with id=" + id; } + @RequestMapping(value = "/foos/{fooid}/bar/{barid}") + @ResponseBody + public String getFoosBySimplePathWithPathVariables(@PathVariable final long fooid, @PathVariable final long barid) { + return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; + } + // other HTTP verbs @RequestMapping(value = "/foos", method = RequestMethod.POST) @@ -59,6 +59,12 @@ public class FooController { return "Get some Foos with Header"; } + @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" }) + @ResponseBody + public String getFoosAsJsonFromREST() { + return "Get some Foos with Header Exact"; + } + // advanced - multiple mappings @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) @@ -79,4 +85,10 @@ public class FooController { return "Fallback for All Requests"; } + @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) + @ResponseBody + public String putAndPostFoos() { + return "Advanced - PUT and POST within single method"; + } + }