From b7d6a5377a2f3a823e406fa4bb2aef0fb46120fa Mon Sep 17 00:00:00 2001 From: eugenp Date: Wed, 15 May 2013 22:39:23 +0300 Subject: [PATCH] mapping work --- .../baeldung/spring/web/config/MvcConfig.java | 6 +- .../spring/web/controller/BarController.java | 37 ++++++++ .../spring/web/controller/FooController.java | 85 ++++++++++++++----- 3 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java diff --git a/spring-mvc/src/main/java/org/baeldung/spring/web/config/MvcConfig.java b/spring-mvc/src/main/java/org/baeldung/spring/web/config/MvcConfig.java index 093a785b48..1325da321d 100644 --- a/spring-mvc/src/main/java/org/baeldung/spring/web/config/MvcConfig.java +++ b/spring-mvc/src/main/java/org/baeldung/spring/web/config/MvcConfig.java @@ -9,8 +9,8 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; @ComponentScan({ "org.baeldung.spring.web.controller" }) public class MvcConfig { - public MvcConfig() { - super(); - } + public MvcConfig() { + super(); + } } \ No newline at end of file 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 new file mode 100644 index 0000000000..def8ffd11e --- /dev/null +++ b/spring-mvc/src/main/java/org/baeldung/spring/web/controller/BarController.java @@ -0,0 +1,37 @@ +package org.baeldung.spring.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class BarController { + + public BarController() { + super(); + } + + // API + + // with @RequestParam + + @RequestMapping(value = "/bars") + @ResponseBody + public String getBarBySimplePathWithRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = "id") + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = { "id", "second" }) + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + +} 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 3698cc24d5..2f86327fc8 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 @@ -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.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @@ -8,28 +9,74 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller public class FooController { - public FooController() { - super(); - } + public FooController() { + super(); + } - // API + // API - @RequestMapping(value = "/foos") - @ResponseBody - public String getFoos() { - return "Get some Foos"; - } + // by paths - @RequestMapping(value = "/foos", method = RequestMethod.POST) - @ResponseBody - public String getFoosPost() { - return "Post some Foos"; - } + @RequestMapping(value = "/foos") + @ResponseBody + public String getFoosBySimplePath() { + return "Simple Get some Foos"; + } - @RequestMapping(value = "/foos", method = RequestMethod.POST, headers = "key=val") - @ResponseBody - public String getFoosWithHeader() { - return "Get some Foos with Header"; - } + // @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) { + return "Get a specific Foo with id=" + id; + } + + // other HTTP verbs + + @RequestMapping(value = "/foos", method = RequestMethod.POST) + @ResponseBody + public String postFoos() { + return "Post some Foos"; + } + + // with headers + + @RequestMapping(value = "/foos", headers = "key=val") + @ResponseBody + public String getFoosWithHeader() { + return "Get some Foos with Header"; + } + + @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" }) + @ResponseBody + public String getFoosWithHeaders() { + return "Get some Foos with Header"; + } + + // advanced - multiple mappings + + @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) + @ResponseBody + public String getFoosOrBarsByPath() { + return "Advanced - Get some Foos or Bars"; + } + + @RequestMapping(value = "*") + @ResponseBody + public String getFallback() { + return "Fallback for GET Requests"; + } + + @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST }) + @ResponseBody + public String allFallback() { + return "Fallback for All Requests"; + } }