@RequestHeader
取得request header參數
用法: 只能加在方法的參數上
用途: 取得request header裡面的參數
request header key:value 用冒號隔開
練習
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test2(@RequestHeader String info) {
System.out.println("header info: " + info);
return "hello world";
}
}
Talend API Tester
add header >info:hello

console:
name(or value): 指定request header 的header 名字 (在header很常用)
Content-Type:application/json
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test2(@RequestHeader(name = "Content-Type") String contentType) {
System.out.println("header info: " + info);
return "hello world";
}
}
required: 是否是必須的header (預設是true)
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test2(@RequestHeader(required = false) String contentType) {
System.out.println("header info: " + info);
return "hello world";
}
}
defaultValue: required= false 的加強版 ,提供預設值
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test2(@RequestHeader(defaultValue = "application/json") String contentType) {
System.out.println("header info: " + info);
return "hello world";
}
}
request header 中的 Contetn-Type
透過Header 中的contentType 知道request body使用的是json格式來傳遞參數

常見的request header
- Content-Type 表示request body 的格式
- application/json (json格式 ,常用)
- application/octet-stream (用於上傳文件)
- multipart/form-data (用於上傳圖片)
- Authorization 用於身分驗證