跳转至

總結

http 協議 規定前後端之間的資料傳輸格式

  • http request 請求
    • http method ex: GET
    • url ex: http://localhost:8080/test
    • requset header
    • request body
  • http response 回應
    • http status code ex: 200
    • response header ex: Content-type:text/plain
    • response body ex: Hello!

url 路徑的對應

  • url格式
  • @RequestMapping

結構化的呈現返回數據

  • json
  • @RestController 、@Controller + @ResponseBody

http method

  • GET
  • POST

取得請求參數的方法

@RequestParam >取得url參數

@RequestBody >取得request body參數

@RequestHeader >取得request header參數

@PathVariable >取得url路徑的值

如何設計 RESTful API

  • REST 風格: 簡化溝通成本

如何實作 RESTful API

  • @GetMapping 、@PostMapping

驗證請求參數的方法

  • @NotNull、@NotBlank、@NotEmpty
  • @Valid、@Validated

常見的http狀態碼

  • 依首位數字分成五大類
  • ReponseEntity<?>

Controller 層統一 的 Exception 處理

  • @ControllerAdvice + @ExceptionHandler
  • 底層使用 Spring AOP 機制

攔截器 Interceptor

  • preHandle 方法的返回值: true = 允許通過 ,false = 拒絕