@RequestMapping(value = "/ex/foos") @ResponseBody public String getFoosBySimplePath() { return "Get some Foos"; }
@RequestMapping(value = "/ex/foos", method = RequestMethod.POST) @ResponseBody public String postFoos() { return "Post some Foos"; }
@RequestMapping(value = "/ex/foos", headers = "key=val") @ResponseBody public String getFoosWithHeader() { return "Get some Foos with Header"; }
@RequestMapping(value = "/ex/foos", headers = { "key1=val1", "key2=val2" }) @ResponseBody public String getFoosWithHeaders() { return "Get some Foos with Header"; }
@RequestMapping(value = "/ex/foos", method = GET, headers = "Accept=application/json") @ResponseBody public String getFoosAsJsonFromBrowser() { return "Get some Foos with Header Old"; }
@RequestMapping(value = "/ex/foos", method = RequestMethod.GET, produces = "application/json") @ResponseBody public String getFoosAsJsonFromREST() { return "Get some Foos with Header New"; }
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'fooController' bean method public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromREST() to {[/ex/foos],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'fooController' bean method public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromBrowser() mapped.
@RequestMapping(value = "/ex/foos/{id}") @ResponseBody public String getFoosBySimplePathWithPathVariable(@PathVariable("id") long id) { return "Get a specific Foo with id=" + id; }
如果方法的参数名和路径里的变量名一致,可用没有值的@PathVariable 来简化:
@RequestMapping(value = "/ex/foos/{id}") @ResponseBody public String getFoosBySimplePathWithPathVariable(@PathVariable String id) { return "Get a specific Foo with id=" + id; }
@RequestMapping(value = "/ex/foos/{fooid}/bar/{barid}") @ResponseBody public String getFoosBySimplePathWithPathVariables (@PathVariable long fooid, @PathVariable long barid) { return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; }
@RequestMapping(value = "/ex/bars/{numericId:[\d]+}") @ResponseBody public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { return "Get a specific Bar with id=" + numericId; }
@RequestMapping(value = "/ex/bars") @ResponseBody public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) { return "Get a specific Bar with id=" + id; }
@RequestMapping(value = "/ex/bars", params = "id") @ResponseBody public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) { return "Get a specific Bar with id=" + id; }
@RequestMapping(value = "/ex/bars", params = { "id", "second" }) @ResponseBody public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") long id) { return "Narrow Get a specific Bar with id=" + id; }
@RequestMapping(value = { "/ex/advanced/bars", "/ex/advanced/foos" }) @ResponseBody public String getFoosOrBarsByPath() { return "Advanced - Get some Foos or Bars"; }
@RequestMapping(value = "/ex/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) @ResponseBody public String putAndPostFoos() { return "Advanced - PUT and POST within single method"; }
@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"; }
7. Spring Configuration
package org.baeldung.spring.web.controller; @Controller public class FooController { ... }
@Configuration @EnableWebMvc @ComponentScan({ "org.baeldung.spring.web.controller" }) public class MvcConfig { // }