Spring MVC註解大全
目前的主流應用都是前後端分離,我們主要關注於基於REST的控制器。當前例子基於Spring Boot 2.1.1.RELEASE
。
1. @Controller
將當前類註冊為Spring MVC
的控制器。
2. @RestController
將當前類註冊為Spring MVC
的REST控制器,它組合類一個@ResponseBody
。
@RestController public class PersonRestController { }
3. @RequestMapping
通用的路徑到MVC方法的對映,可註解在類上或方法上,支援GET
POST
PUT
DELETE
PATCH
,我們平常應該使用專用的註解,@RequestMapping
放置在類一級。
@RestController @RequestMapping("/people") public class PersonRestController { }
4. @PathVariable
可以在請求路徑中獲取變數。
@GetMapping("/{id}") public void get(@PathVariable Long id){ log.info("--------" + id + "--------"); }
訪問路徑為:http://localhost/people/1
5. @RequestParam
獲取請求引數。
@GetMapping("/findByName") public void findByName(@RequestParam String name){ log.info("--------" + name + "--------"); }
訪問路徑為:http://localhost/people/findByName?name=wyf
6. @RequestHeader
獲取請求頭
的資訊。
@GetMapping("/headerInfo") public void header(@RequestHeader("User-Agent") String userAgent){ log.info("--------" + userAgent + "--------"); }
7. @CookieValue
獲取客戶端的cookie中的資訊。
@GetMapping("/getCookieValue") public void getCookieValue(@CookieValue("tz") String timeZone){ log.info("--------" + timeZone + "--------"); }
8. @RequestBody
從請求體
中獲取資料。
@PostMapping public void save(@RequestBody Person person){ log.info("--------" + person + "--------"); }
9. @ResponseBody
將返回資料寫入返回體
中,@RestController
已包含。
10. @GetMapping
對映Http
的GET
方法,獲取資訊。
@GetMapping("/{id}") public void get(@PathVariable Long id){}
11. @PostMapping
對映Http
的POST
方法,儲存資訊。
PostMapping public void save(@RequestBody Person person){}
12. @PutMapping
對映Http
的PUT
方法,更新資訊。
@PutMapping("/{id}") public void update(@PathVariable Long id , @RequestBody Person person){}
13. @DeleteMapping
對映Http
的DELETE
方法,刪除資訊。
@DeleteMapping("/{id}") public void remove(@PathVariable Long id){}
14. @PatchMapping
對映Http
的PATCH
方法,更新部分資訊。
@PatchMapping public void patch(@PathVariable Long id , @RequestBody Person person){}
15.@ControllerAdvice
用來處理跨控制器的統一處理,如:異常處理(ExceptionHandler
),資料轉換(@InitBinder
)等,它組合了一個@Component
註解,所以不用再手工註冊bean。
@ControllerAdvice public class WiselyControllerAdvice {}
16. @ExceptionHandler
針對控制器的全域性異常處理。
@ControllerAdvice public class WiselyControllerAdvice { @ExceptionHandler(Exception.class) @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public String defaultExceptionHandler(HttpServletRequest request, Exception exception) { return "伺服器異常"; } @ExceptionHandler(IOException.class) public ResponseEntity<String> IOExceptionnHandler(HttpServletRequest request, Exception exception) { return new ResponseEntity<String>("伺服器IO異常",HttpStatus.INTERNAL_SERVER_ERROR); } }
控制器中的演示:
@GetMapping("/exception") public void exception() throws Exception { throw new Exception(); } @GetMapping("/ioException") public void ioException() throws IOException { throw new IOException(); }
17.@InitBinder
將制定格式的字串專程物件,如將2-wyf
(id-name),轉換成dog物件。
@ControllerAdvice public class WiselyControllerAdvice { @InitBinder public void dataBinding(WebDataBinder binder) { binder.registerCustomEditor(Dog.class, new DogPropertiesEditor()); } }
自定義的屬性編輯器:
public class DogPropertiesEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { String[] array = text.split("-"); Long id = new Long(array[0]); String name = array[1]; setValue(new Dog(id , name)); } }
演示控制器:
@GetMapping("/binder/{dog-txt}") public void binder(@PathVariable("dog-txt") Dog dog) { log.info("--------" + dog + "--------"); }
訪問地址:http://localhost/binder/2-wyf
18. @ResponseStatus
定製Http的Reponse的狀態:
@GetMapping("/showHttpStatus") @ResponseStatus(HttpStatus.ALREADY_REPORTED) public void showHttpStatus(){}
19. @CrossOrigin
在不同的域下的網頁用指令碼呼叫介面的時候存在這跨域的問題,可以用當前註解來允許可以跨域的域。
@GetMapping("/{id}/cross") @CrossOrigin({"*"}) public void crossGet(@PathVariable Long id){}
20. @RequestPart
從引數重獲取上傳的檔案:
@PostMapping("/upload") public void upload(@RequestPart("file") MultipartFile image){ log.info("--------" + image.getOriginalFilename() + "--------"); }