1. 程式人生 > >SpringMVC註解

SpringMVC註解

Controller引數

  • @PathVariable:從URI的佔位符中獲取變數值;
@GetMapping("/owners/{ownerId}/pets/{petId}")
public Pet findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
    // ...
}
  • @RequestParam:從Get的查詢引數或者Put的Form表單中獲取變數值;
@GetMapping("/owners/{ownerId}?petId=10")
public Pet findPet(@PathVariable Long ownerId, @RequestParam("petId") int petId) {
    // ...
}
  • @RequestHeader:從請求頭中獲取變數的值;
@GetMapping("/demo")
public void handle(
        @RequestHeader("Accept-Encoding") String encoding, 
        @RequestHeader("Keep-Alive") long keepAlive) { 
    //...
}
  • @RequestBody:將請求體的內容經過HttpMessageConverter反序列化為Java物件;
@PostMapping("/accounts")
public void handle(@RequestBody Account account) {
    // ...
}
  • @ResponseBody:將返回型別經過HttpMessageConverter序列化到響應體中;
@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
    // ...
}

Controller的入參還支援很多其他型別,比如ServletRequest、ServletResponse和HttpSession等等,具體可檢視官網