mapping work

This commit is contained in:
eugenp 2013-05-15 22:39:23 +03:00
parent 4a1370d1dd
commit b7d6a5377a
3 changed files with 106 additions and 22 deletions

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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";
}
}