路徑變量@PathVariable
@RestController
public class ParameterTestController {
/**
* 測(cè)試 @PathVariable 注解
*/
@GetMapping("/car/{carId}/owner/{userName}")
public Map<String, Object> getCar(@PathVariable("carId") Integer carId,
@PathVariable("userName") String userName,
@PathVariable Map<String, String> allPathVariable) {
HashMap<String, Object> map = new HashMap<>();
map.put("carId", carId);
map.put("userName", userName);
map.put("allPathVariable", allPathVariable);
return map;
}
}
路徑映射注解中用
{}
包裹的變量會(huì)與方法的參數(shù)中用了@PathVariable
注解的變量對(duì)應(yīng)舟扎,然后在方法中就可以直接去使用路徑變量了-
查看
@PathVariable
注解的源碼注釋如下If the method parameter is Map<String, String> then the map is populated with all path variable names and values.
也就是說(shuō)惧财,如果方法的參數(shù)中有一個(gè)
Map<String, String>
類型的參數(shù)的話趾诗,這個(gè)參數(shù)會(huì)被自動(dòng)裝配,內(nèi)容就是所有的路徑變量的key和value
訪問(wèn)結(jié)果
測(cè)試路徑變量注解 -- @PathVariable
獲取請(qǐng)求頭@RequestHeader
/**
* 測(cè)試 @RequestHeader 注解
*/
@GetMapping("/get-header")
public Map<String, Object> getHeaders(@RequestHeader("User-Agent") String userAgent,
@RequestHeader MultiValueMap<String, String> multiValueMap,
@RequestHeader HttpHeaders httpHeaders) {
HashMap<String, Object> map = new HashMap<>();
map.put("userAgent", userAgent);
map.put("multiValueMap", multiValueMap);
map.put("httpHeaders", httpHeaders);
return map;
}
-
類似地,該注解源碼注釋中有提到
If the method parameter is Map<String, String>, MultiValueMap<String, String>, or HttpHeaders then the map is populated with all header names and values.
所以這里測(cè)試的時(shí)候添加了三個(gè)參數(shù),分別是
Map<String, String>
類型素邪、MultiValueMap<String, String>
類型和HttpHeaders
類型的參數(shù),然后返回給瀏覽器后查看是否真的自動(dòng)裝配了所有的headers
測(cè)試結(jié)果
測(cè)試獲取請(qǐng)求頭注解 -- @RequestHeader
獲取請(qǐng)求參數(shù)@RequestParam
/**
* 測(cè)試 @RequestParam 注解
*/
@GetMapping("/get-request-param")
public Map<String, Object> getRequestParam(@RequestParam("username") String userName,
@RequestParam Map<String, String> commonMap,
@RequestParam MultiValueMap<String, String> multiValueMap) {
HashMap<String, Object> map = new HashMap<>();
map.put("username", userName);
map.put("commonMap", commonMap);
map.put("multiValueMap", multiValueMap);
return map;
}
-
與前兩個(gè)注解類似
If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.
測(cè)試結(jié)果
測(cè)試獲取請(qǐng)求參數(shù)注解 -- @RequestParam
獲取cookie值@CookieValue
/**
* 測(cè)試 @CookieValue 注解
*/
@GetMapping("/get-cookie-value")
public Map<String, Object> getCookieValue(@CookieValue("csrftoken") String csrfToken,
@CookieValue("csrftoken") Cookie csrfTokenCookie) {
HashMap<String, Object> map = new HashMap<>();
map.put("csrftoken-string", csrfToken);
map.put("csrftoken-cookie-obj", csrfTokenCookie);
String info = csrfTokenCookie.getName() + " ===> " + csrfTokenCookie.getValue();
map.put("csrftoken-cookie-obj-info", info);
return map;
}
-
官方文檔注釋中提到
The method parameter may be declared as type javax.servlet.http.Cookie or as cookie value type (String, int, etc.).
也就是說(shuō)參數(shù)中可以接收一個(gè)java原生的servlet Cookie對(duì)象
測(cè)試結(jié)果
測(cè)試獲取cookie值注解 -- @CookieValue
獲取請(qǐng)求體@RequestBody
- 對(duì)于
POST
猪半、PUT
兔朦、PATCH
等有請(qǐng)求體的請(qǐng)求,要獲取請(qǐng)求體的數(shù)據(jù)磨确,就用該注解
/**
* 測(cè)試 @RequestBody 注解
*/
@PostMapping("/post-request-body")
public Map<String, Object> getRequestBody(@RequestBody String requestBody) {
HashMap<String, Object> map = new HashMap<>();
map.put("requestBody", requestBody);
return map;
}
測(cè)試結(jié)果
請(qǐng)求表單
請(qǐng)求結(jié)果
獲取request請(qǐng)求域@RequestAttribute
- 使用模板語(yǔ)言的時(shí)候才會(huì)用到這個(gè)沽甥,前后端分離的話就用不上
- 可以獲取原生
HttpServletRequest
中setAttribute
方法設(shè)置的屬性