GET
使用@RequestParam注解
可以在控制器方法的參數(shù)上使用@RequestParam注解來獲取請求中的參數(shù)值。例如:
@GetMapping("/example")
public String example(@RequestParam String param) {
// 使用param參數(shù)的值
return "Value of param: " + param;
}
可以為@RequestParam注解的參數(shù)提供默認(rèn)值,以處理參數(shù)缺失的情況。例如:
@GetMapping("/example")
public String example(@RequestParam(defaultValue = "default") String param) {
// 使用param參數(shù)的值,如果參數(shù)未提供,則使用默認(rèn)值"default"
return "Value of param: " + param;
}
可以使用@RequestParam Map<String, String>來獲取所有的請求參數(shù)鍵值對。例如:
@GetMapping("/example")
public String example(@RequestParam Map<String, String> params) {
String param = params.get("param");
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用@PathVariable注解
如果參數(shù)是作為路徑的一部分傳遞的,可以使用@PathVariable注解來獲取參數(shù)值。例如:
@GetMapping("/example/{param}")
public String example(@PathVariable String param) {
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用HttpServletRequest對象
可以在控制器方法的參數(shù)列表中添加HttpServletRequest對象,然后從中獲取請求參數(shù)。例如:
@GetMapping("/example")
public String example(HttpServletRequest request) {
String param = request.getParameter("param");
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用@ModelAttribute注解
可以使用@ModelAttribute注解來將請求參數(shù)綁定到一個自定義對象中。例如:
@GetMapping("/example")
public String example(@ModelAttribute MyObject myObject) {
String param = myObject.getParam();
// 使用param參數(shù)的值
return "Value of param: " + param;
}
?POST
使用@RequestParam注解
可以在控制器方法的參數(shù)上使用@RequestParam注解來獲取請求體中的參數(shù)值。默認(rèn)情況下,@RequestParam用于獲取表單數(shù)據(jù)。例如:
@PostMapping("/example")
public String example(@RequestParam String param) {
// 使用param參數(shù)的值
return "Value of param: " + param;
}
可以使用@RequestParam Map<String, String>來獲取所有的請求參數(shù)鍵值對。這對于處理不固定參數(shù)的情況很有用。例如:
@PostMapping("/example")
public String example(@RequestParam Map<String, String> params) {
String param = params.get("param");
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用@RequestBody注解
如果請求體是JSON或其他格式的數(shù)據(jù),可以使用@RequestBody注解將請求體映射到一個自定義對象中。例如:
@PostMapping("/example")
public String example(@RequestBody MyObject myObject) {
String param = myObject.getParam();
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用HttpServletRequest對象
可以在控制器方法的參數(shù)列表中添加HttpServletRequest對象,然后從中獲取請求參數(shù)。這對于處理復(fù)雜的請求體非常有用。例如:
@PostMapping("/example")
public String example(HttpServletRequest request) {
String param = request.getParameter("param");
// 使用param參數(shù)的值
return "Value of param: " + param;
}
使用@ModelAttribute注解
與POST請求中的表單數(shù)據(jù)一起使用@ModelAttribute注解,將請求參數(shù)綁定到一個自定義對象中。例如:文章來源:http://www.zghlxwxcb.cn/news/detail-690807.html
@PostMapping("/example")
public String example(@ModelAttribute MyObject myObject) {
String param = myObject.getParam();
// 使用param參數(shù)的值
return "Value of param: " + param;
}
類似于@RequestParam Map,也可以使用@ModelAttribute注解的Map參數(shù)來獲取請求參數(shù)鍵值對。例如:文章來源地址http://www.zghlxwxcb.cn/news/detail-690807.html
@PostMapping("/example")
public String example(@ModelAttribute("myObject") Map<String, String> params) {
String param = params.get("param");
// 使用param參數(shù)的值
return "Value of param: " + param;
}
到了這里,關(guān)于Springboot GET和POST請求的常用參數(shù)獲取方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!