Spring Boot註解 之 @RequestX

weixin_34402408發表於2018-03-09

本文的示例程式碼參考RequestX

目錄

開始

spring init -dweb --build gradle RequestX

@RequestMapping

GET

# cd RequestX
vim src/main/java/com/example/RequestX/UserController.java
package com.example.RequestX;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @RequestMapping(path = "/users", method = RequestMethod.GET)
    public String getUsers() {
        return "getUsers";
    }
}
  • 測試
./gradlew bootrun

curl localhost:8080/users # 返回"getUsers"

@RequestMapping(method = RequestMethod.GET)等價於@GetMapping

POST

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
public class UserController {
    @RequestMapping(path = "/users", method = RequestMethod.GET)
    public String getUsers() {
        return "getUsers";
    }

    @RequestMapping(path = "/users", method = RequestMethod.POST)
    public String addUser() {
        return "addUser";
    }
}
  • 測試
./gradlew bootrun

curl -X POST localhost:8080/users # 返回"addUser"

@RequestMapping(method = RequestMethod.POST)等價於@PostMapping

小重構

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public String getUsers() {
        return "getUsers";
    }

    @PostMapping
    public String addUser() {
        return "addUser";
    }
}

  • 測試
./gradlew bootrun

curl localhost:8080/users # 返回"getUsers"

curl -X POST localhost:8080/users # 返回"addUser"

@RequestHeader

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public String getUsers(@RequestHeader("Custom-Header") String customHeader) {
        System.out.println("Custom-Header: " + customHeader);
        return "getUsers";
    }

    # 省略了未修改程式碼
}
  • 測試
./gradlew bootrun

curl -H 'Custom-Header: kevin' localhost:8080/users # 列印"Custom-Header: kevin" + 返回"getUsers"

@RequestBody

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
@RequestMapping("/users")
public class UserController {
    # 省略了未修改程式碼

    @PostMapping
    public String addUser(@RequestBody String requestBody) {
        System.out.println("requestBody: " + requestBody);
        return "addUser";
    }
}
  • 測試
./gradlew bootrun

curl -X POST -d 'kevin' localhost:8080/users # 列印"requestBody: kevin=" + 返回"addUser"

application/x-www-form-urlencoded

curl -X POST -d 'name=kevin' -x 127.0.0.1:8888 localhost:8080/users
# 列印"requestBody: name=kevin" + 返回"addUser"

此時 HTTP請求的抓包資訊如下

2180450-7f9d50ac994a2f7e.png
image.png

注意 Content-Type: application/x-www-form-urlencoded

它是瀏覽器原生<form>表單提交資料的編碼方式 也是很多API和工具POST請求預設的編碼方式 形如

key1=val1&key2=val2

這裡使用Charles抓包工具 更多參考 Charles 從入門到精通

application/json

curl -X POST -H "Content-type: application/json" -d '{"name":"kevin"}' -x 127.0.0.1:8888 http://localhost:8080/users
# 列印"requestBody: {"name":"kevin"}" + 返回"addUser"

此時 HTTP請求的抓包資訊如下

2180450-fc493e8a5df2ac21.png
image.png

注意 Content-Type: application/json

關於HTTP請求Body的這兩種編碼方式的比較 更多參考為什麼介面設計都不用普通 POST ?

@RequestParam

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public String getUsers(@RequestParam(value = "name", required = false, defaultValue = "jason") String name) {
        System.out.println("name: " + name);
        return "getUsers";
    }

    # 省略了未修改程式碼
}
  • 測試
./gradlew bootrun

curl localhost:8080/users?name=kevin # 列印"name: kevin" + 返回"getUsers"

@PathVariable

vim src/main/java/com/example/RequestX/UserController.java
# 省略了包名和匯入檔案

@RestController
@RequestMapping("/users")
public class UserController {
    # 省略了未修改程式碼

    @GetMapping("/{userId}")
    public String getUser(@PathVariable("userId") String userId) {
        System.out.println("userId: " + userId);
        return "getUser";
    }

    # 省略了未修改程式碼
}
  • 測試
./gradlew bootrun

curl localhost:8080/users/123 # 列印"userId: 123" + 返回"getUsers"

參考

相關文章