欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot如何接收get和post請求參數(shù)

 更新時間:2023年06月28日 10:16:03   作者:Archie_java  
這篇文章主要介紹了springboot如何接收get和post請求參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、get請求前端傳參和后臺接收的寫法:

1、ajax請求:

 $.ajax({                
     	url:'/user/login3',
        //contentType: 'application/json;charset=utf-8', //這一句添加與否,效果都一樣
     	data:{"userName":"張三", "userPass":"123"},
     	method:'get',       
     	dataType:'json',                
     	success:function(res){    
     		if(res.successful == true || res.successful=='true'){      
     			alert("ok");   
     		}else{
     			alert("用戶名或密碼錯誤");            
     		}
     	},                
     	error:function (data) {
     		alert("用戶名或密碼錯誤");            
     	}            
     });

此時查看瀏覽器的network選項(xiàng),會看到請求地址變成:

? Request URL:http://localhost:8082/user/login3?userName=%E5%BC%A0%E4%B8%89&userPass=123

即請求參數(shù)拼接到url地址欄后,并且已經(jīng)編碼了。

2、針對這種請求,后臺接收參數(shù)有三種寫法:

1)、參數(shù)名稱與前端key對應(yīng),不加任何注解:

@RequestMapping(value = "/login3", method = RequestMethod.GET)
	Map<String, Object> login4( String userName,  String userPass)

2)、參數(shù)名稱與前端key對應(yīng),添加@RequestParam注解:

@RequestMapping(value = "/login3", method = RequestMethod.GET)
	Map<String, Object> login2(@RequestParam String userName, @RequestParam String userPass) 

3)、使用@RequestParam注解,將所有的參數(shù)封裝到Map<String,Object>對象:

@RequestMapping(value = "/login3", method = RequestMethod.GET)
	Map<String, Object> login3(@RequestParam Map<String,Object> param)

此時,后臺通過param.get(“userName”)來獲取參數(shù)的值。@RequestParam注解換成@RequestBody注解無效,ajax請求報400錯誤,進(jìn)入不了后臺對應(yīng)的方法。

二、post請求前端傳參和后臺接收的寫法:

? 3、ajax請求:

 $.ajax({                
     	url:'/user/login3',
     	data:{"userName":"張三", "userPass":"123"},
     	method:'post',       
     	dataType:'json',                
     	success:function(res){    
     		if(res.successful == true || res.successful=='true'){      
     			alert("ok");   
     		}else{
     			alert("用戶名或密碼錯誤");            
     		}
     	},                
     	error:function (data) {
     		alert("用戶名或密碼錯誤");            
     	}            
     });

4、針對3的ajax請求,后臺的接口參數(shù)寫法同2。

5、ajax請求:

$.ajax({                
     	url:'/user/login3',
     	contentType: 'application/json;charset=utf-8',
     	data:JSON.stringify({"userName":"張三", "userPass":"123"}),
     	method:'post',       
     	dataType:'json',   

不同的地方在于添加了contentType,data需要改成序列化的json字符串。

6、針對5這種ajax請求,2的三種寫法后臺都拿不到值,或者直接報400錯誤。需要用另外一種寫法:

@RequestMapping(value = "/login3", method = RequestMethod.POST)
	Map<String, Object> login3(@RequestBody Map<String,Object> param) {
		try {
			System.out.println("userName==" + param.get("userName") +", userPass==" + param.get("userPass"));

? 這里的param名稱可以隨便取,可以用別的名稱代替;Map<String,Object>也可以替換成自定義的POJO類名,比如自定了User類,包含了userName和userPass兩個屬性,則@RequestBody User user也可以去到前端傳遞的值,此時用user.getUserName()就可以了。

或者:

@RequestMapping(value = "/login2", method = RequestMethod.POST)
	Map<String, Object> login2(@RequestBody String userInfo) {
		try {
			JSONObject json = JSONObject.parseObject(userInfo);
			System.out.println("userName==" + json.getString("userName") +", userPass==" + json.getString("userPass"));

這里的userInfo名稱是隨便取,可以用別的名稱代替;把這里的@RequestBody替換成@RequestParam,會報400錯誤。

到此這篇關(guān)于springboot接收get和post請求參數(shù)的文章就介紹到這了,更多相關(guān)springboot接收get和post參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論