@RequestMapping 注解:
@RequestMapping 是 Spring Web 應用程序中最常被用到的注解之一。這個注解會將 HTTP 請求映射到 MVC 和 REST 控制器的處理方法上。
Request Mapping 基礎用法
在 Spring MVC 應用程序中,RequestDispatcher (在 Front Controller 之下) 這個 servlet 負責將進入的 HTTP 請求路由到控制器的處理方法。
在對 Spring MVC 進行的配置的時候, 你需要指定請求與處理方法之間的映射關系。
要配置 Web 請求的映射,就需要你用上 @RequestMapping 注解。
@RequestMapping 注解可以在控制器類的級別和/或其中的方法的級別上使用。
在類的級別上的注解會將一個特定請求或者請求模式映射到一個控制器之上。之后你還可以另外添加方法級別的注解來進一步指定到處理方法的映射關系。
下面是一個同時在類和方法上應用了 @RequestMapping 注解的示例:
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping("/") String get() { //mapped to hostname:port/home/ return "Hello from get"; } @RequestMapping("/index") String index() { //mapped to hostname:port/home/index/ return "Hello from index"; } }
如上述代碼所示,到 /home 的請求會由 get() 方法來處理,而到 /home/index 的請求會由 index() 來處理。
@RequestMapping 來處理多個 URI
你可以將多個請求映射到一個方法上去,只需要添加一個帶有請求路徑值列表的 @RequestMapping 注解就行了。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = { "", "/page", "page*", "view/*,**/msg" }) String indexMultipleMapping() { return "Hello from index multiple mapping."; } }
如你在這段代碼中所看到的,@RequestMapping 支持統配符以及ANT風格的路徑。前面這段代碼中,如下的這些 URL 都會由 indexMultipleMapping() 來處理:
localhost:8080/home localhost:8080/home/ localhost:8080/home/page localhost:8080/home/pageabc localhost:8080/home/view/ localhost:8080/home/view/view
@RequestParam
帶有 @RequestParam 的 @RequestMapping
@RequestParam 注解配合 @RequestMapping 一起使用,可以將請求的參數同處理方法的參數綁定在一起。
@RequestParam 注解使用的時候可以有一個值,也可以沒有值。這個值指定了需要被映射到處理方法參數的請求參數, 代碼如下所示:
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/id") String getIdByValue(@RequestParam("id") String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL with value element"; } @RequestMapping(value = "/personId") String getId(@RequestParam String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL without value element"; } }
@RequestParam 的 defaultValue 取值就是用來給取值為空的請求參數提供一個默認值的。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/name") String getName(@RequestParam(value = "person", defaultValue = "John") String personName) { return "Required element of request param"; } }
在這段代碼中,如果 person 這個請求參數為空,那么 getName() 處理方法就會接收 John 這個默認值作為其參數。
@RequestMapping屬性:
method :
Spring MVC 的 @RequestMapping 注解能夠處理 HTTP 請求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的請求默認都會是 HTTP GET 類型的。
對請求的映射不僅僅不局限在標示的方法的返回值對請求url上,還可以對請求的其屬性做出約定,如請求的method,是get還是post。如果做出了method的條件限定,當請求的url即使映射上了,method不符合的話也不能生成物理視圖並轉發到目標頁面。你需要在 @RequestMapping 中使用 method 來聲明 HTTP 請求所使用的方法類型:
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(method = RequestMethod.GET) String get() { return "Hello from get"; } @RequestMapping(method = RequestMethod.DELETE) String delete() { return "Hello from delete"; } @RequestMapping(method = RequestMethod.POST) String post() { return "Hello from post"; } @RequestMapping(method = RequestMethod.PUT) String put() { return "Hello from put"; } @RequestMapping(method = RequestMethod.PATCH) String patch() { return "Hello from patch"; } }
produces:
它的作用是指定返回值類型,不但可以設置返回值類型還可以設定返回值的字符編碼;
@Controller @RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8") @ResponseBody public Pet getPet(@PathVariable String petId, Model model) { // implementation omitted }
consumes:
指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;
@Controller @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json") public void addPet(@RequestBody Pet pet, Model model) { // implementation omitted }
header:
根據請求中的消息頭內容縮小 請求映射 的范圍;
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/head", headers = { "content-type=text/plain" }) String post() { return "Mapping applied along with headers"; } }
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/head", headers = { "content-type=text/plain", "content-type=text/html" }) String post() { return "Mapping applied along with headers"; } }
params :
params 元素可以進一步幫助我們縮小請求映射的定位范圍(定義傳參的值,當值為定義的值時,進入方法運行,否則不運行)。使用 params 元素,你可以讓多個處理方法處理到同一個URL 的請求, 而這些請求的參數是不一樣的。你可以用 myParams = myValue 這種格式來定義參數,也可以使用通配符來指定特定的參數值在請求中是不受支持的。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/fetch", params = { "personId=10" }) String getParams(@RequestParam("personId") String id) { return "Fetched parameter using params attribute = " + id; } @RequestMapping(value = "/fetch", params = { "personId=20" }) String getParamsDifferent(@RequestParam("personId") String id) { return "Fetched parameter using params attribute = " + id; } }
@RequestMapping 快捷方式 :
Spring 4.3 引入了方法級注解的變體,也被叫做 @RequestMapping 的組合注解。組合注解可以更好的表達被注解方法的語義。它們所扮演的角色就是針對 @RequestMapping 的封裝,而且成了定義端點的標准方法。
例如,@GetMapping 是一個組合注解,它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一個快捷方式。
方法級別的注解變體有如下幾個:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
如下代碼展示了如何使用組合注解:
@RestController @RequestMapping("/home") public class IndexController { @GetMapping("/person") public @ResponseBody ResponseEntity < String > getPerson() { return new ResponseEntity < String > ("Response from GET", HttpStatus.OK); } @GetMapping("/person/{id}") public @ResponseBody ResponseEntity < String > getPersonById(@PathVariable String id) { return new ResponseEntity < String > ("Response from GET with id " + id, HttpStatus.OK); } @PostMapping("/person") public @ResponseBody ResponseEntity < String > postPerson() { return new ResponseEntity < String > ("Response from POST method", HttpStatus.OK); } @PutMapping("/person") public @ResponseBody ResponseEntity < String > putPerson() { return new ResponseEntity < String > ("Response from PUT method", HttpStatus.OK); } @DeleteMapping("/person") public @ResponseBody ResponseEntity < String > deletePerson() { return new ResponseEntity < String > ("Response from DELETE method", HttpStatus.OK); } @PatchMapping("/person") public @ResponseBody ResponseEntity < String > patchPerson() { return new ResponseEntity < String > ("Response from PATCH method", HttpStatus.OK); } }
在這段代碼中,每一個處理方法都使用 @RequestMapping 的組合變體進行了注解。盡管每個變體都可以使用帶有方法屬性的 @RequestMapping 注解來互換實現, 但組合變體仍然是一種最佳的實踐 — 這主要是因為組合注解減少了在應用程序上要配置的元數據,並且代碼也更易讀。
文章轉載至:https://blog.csdn.net/sunshine_yg/article/details/80493604