|
| 1 | +package org.baeldung.web.controller; |
| 2 | + |
| 3 | +import org.springframework.stereotype.Controller; |
| 4 | +import org.springframework.web.bind.annotation.PathVariable; |
| 5 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 6 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 7 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 8 | + |
| 9 | +@Controller |
| 10 | +@RequestMapping(value = "/ex") |
| 11 | +public class FooMappingExamplesController { |
| 12 | + |
| 13 | + public FooMappingExamplesController() { |
| 14 | + super(); |
| 15 | + } |
| 16 | + |
| 17 | + // API |
| 18 | + |
| 19 | + // mapping examples |
| 20 | + |
| 21 | + @RequestMapping(value = "/foos") |
| 22 | + @ResponseBody |
| 23 | + public String getFoosBySimplePath() { |
| 24 | + return "Simple Get some Foos"; |
| 25 | + } |
| 26 | + |
| 27 | + // with @PathVariable |
| 28 | + |
| 29 | + @RequestMapping(value = "/foos/{id}") |
| 30 | + @ResponseBody |
| 31 | + public String getFoosBySimplePathWithPathVariable(@PathVariable final long id) { |
| 32 | + return "Get a specific Foo with id=" + id; |
| 33 | + } |
| 34 | + |
| 35 | + @RequestMapping(value = "/foos/{fooid}/bar/{barid}") |
| 36 | + @ResponseBody |
| 37 | + public String getFoosBySimplePathWithPathVariables(@PathVariable final long fooid, @PathVariable final long barid) { |
| 38 | + return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; |
| 39 | + } |
| 40 | + |
| 41 | + // other HTTP verbs |
| 42 | + |
| 43 | + @RequestMapping(value = "/foos", method = RequestMethod.POST) |
| 44 | + @ResponseBody |
| 45 | + public String postFoos() { |
| 46 | + return "Post some Foos"; |
| 47 | + } |
| 48 | + |
| 49 | + // with headers |
| 50 | + |
| 51 | + @RequestMapping(value = "/foos", headers = "key=val") |
| 52 | + @ResponseBody |
| 53 | + public String getFoosWithHeader() { |
| 54 | + return "Get some Foos with Header"; |
| 55 | + } |
| 56 | + |
| 57 | + @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" }) |
| 58 | + @ResponseBody |
| 59 | + public String getFoosWithHeaders() { |
| 60 | + return "Get some Foos with Header"; |
| 61 | + } |
| 62 | + |
| 63 | + // @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json") |
| 64 | + // @ResponseBody |
| 65 | + // public String getFoosAsJsonFromBrowser() { |
| 66 | + // return "Get some Foos with Header Old"; |
| 67 | + // } |
| 68 | + |
| 69 | + @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" }) |
| 70 | + @ResponseBody |
| 71 | + public String getFoosAsJsonFromREST() { |
| 72 | + return "Get some Foos with Header New"; |
| 73 | + } |
| 74 | + |
| 75 | + // advanced - multiple mappings |
| 76 | + |
| 77 | + @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) |
| 78 | + @ResponseBody |
| 79 | + public String getFoosOrBarsByPath() { |
| 80 | + return "Advanced - Get some Foos or Bars"; |
| 81 | + } |
| 82 | + |
| 83 | + @RequestMapping(value = "*") |
| 84 | + @ResponseBody |
| 85 | + public String getFallback() { |
| 86 | + return "Fallback for GET Requests"; |
| 87 | + } |
| 88 | + |
| 89 | + @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST }) |
| 90 | + @ResponseBody |
| 91 | + public String allFallback() { |
| 92 | + return "Fallback for All Requests"; |
| 93 | + } |
| 94 | + |
| 95 | + @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) |
| 96 | + @ResponseBody |
| 97 | + public String putAndPostFoos() { |
| 98 | + return "Advanced - PUT and POST within single method"; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments