天天看點

get、post、getMapping、postMapping、requestMapping差別getMapping、postMapping和requestMapping差別

getMapping、postMapping和requestMapping差別

說明@GetMapping就是@RequestMapping附加了get請求方法

@PostMapping同理如上,就是附加了post請求方法

什麼時候使用

  • 前端method中特指了get或post的時候分别使用@GetMapping和@PostMapping
    # 示例
    <form action="testGetMapping" method="get">這裡使用@GetMapping<form/>
    <form action="testPostMapping" method="post">這裡使用@PostMapping<form/>
               
  • 如果傳的參數是@RequestBody ,多參或者傳對象的情況下使用@PostMapping注解
    @PostMapping("/getOrderList")
    public List<Object> getList(@RequestBody List<Object> orderList) {}
               
  • 無參,@RequestParam和@PathVaiable的情況下使用GetMapping
    @gettMapping("/test")
    public ModelAndView test16(@RequestParam("id")Long id){}
               
    @gettMapping("/test/{id}")
    public  ModelAndView (@PathVaiable("name") Long id){}
               

本質上還是Get和Post請求的差別

GET和POST兩種方法都是将資料送到伺服器

若符合下列任一情況,則用POST方法:

  • 請求的結果有持續性的副作用,例如,資料庫内添加新的資料行。
  • 若使用GET方法,則表單上收集的資料可能讓URL過長。
  • 要傳送的資料不是采用7位的ASCII編碼。

​ 若符合下列任一情況,則用GET方法:

  • 請求是為了查找資源,HTML表單資料僅用來幫助搜尋。
  • 請求結果無持續性的副作用。
  • 收集的資料及HTML表單内的輸入字段名稱的總長不超過1024個字元。