Spring Boot註解 之 @RequestX
本文的示例程式碼參考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請求的抓包資訊如下
注意 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請求的抓包資訊如下
注意 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"
參考
相關文章
- Spring boot註解Spring Boot
- spring boot 註解@ComponentSpring Boot
- Spring Boot 自定義註解Spring Boot
- Spring Boot 自定義註解失效Spring Boot
- Spring Boot配置類的註解Spring Boot
- Spring Boot系列十八 Spring AOP + 註解實現統一註解Spring Boot
- Spring Boot之使用Scheduled註解實現定時任務Springboot
- spring boot的常用註解有哪些?Spring Boot
- Spring註解之@ImportSpringImport
- Spring Boot開發(Gradle+註解)Spring BootGradle
- Java之Spring Boot詳解JavaSpring Boot
- Spring 註解程式設計之模式註解Spring程式設計模式
- MyBatis 註解版(五)Spring boot 註解系列 插入物件返回 idMyBatisSpring Boot物件
- Spring Boot 自動配置之條件註解Spring Boot
- Spring Boot 自動配置之組合註解Spring Boot
- spring boot使用註解的方式整合mybaitsSpring BootAI
- spring boot 註解物件的問題 待研究Spring Boot物件
- Spring註解之@ConditionalSpring
- Spring Boot註解@Transactional結合實際例子講解Spring Boot
- Spring系列之新註解配置+Spring整合junit+註解注入Spring
- Spring / Spring boot 基於註解非同步程式設計@AsyncSpring Boot非同步程式設計
- Spring Boot 自動配置之@Enable* 與@Import註解Spring BootImport
- spring boot使用@Async非同步註解,原理+原始碼Spring Boot非同步原始碼
- spring boot啟動掃描不到自定義註解Spring Boot
- Spring 註解面面通 之 @MatrixVariableSpring
- Spring Boot 最核心的 25 個註解,都是乾貨!Spring Boot
- spring boot @Async非同步註解上下文透傳Spring Boot非同步
- Spring Boot Transactional註解原始碼閱讀筆記(二)Spring Boot原始碼筆記
- spring boot 利用註解實現許可權驗證Spring Boot
- Spring Boot Transactional註解原始碼閱讀筆記(一)Spring Boot原始碼筆記
- Spring-Boot專案中配置redis註解快取SpringbootRedis快取
- Spring原始碼學習之:spring註解@TransactionalSpring原始碼
- Spring Boot 動態資料來源(Spring 註解資料來源)Spring Boot
- Spring Boot @Condition 註解,組合條件你知道嗎Spring Boot
- 專題一之Spring Boot入門詳解Spring Boot
- Spring註解Spring
- Spring Boot最常用的25個註解,乾貨瞭解一下Spring Boot
- Spring Boot之搞定mongoTemplateSpring BootGo