前言
參數綁定,簡單來說就是客戶端發送請求,而請求中包含一些數據,那么這些數據怎么到達 controller ?這在實際項目開發中也是用到的最多的,那么 springmvc 的參數綁定是怎么實現的呢?
下面我們來詳細的講解。
springmvc參數綁定,簡單來說就是將客戶端請求的key/value數據綁定到controller方法的形參上,然后就可以在controller中使用該參數了
下面通過5個常用的注解演示下如何進行參數綁定:
1. @pathvariable注解
@pathvariable 是用來獲得請求url中的動態參數的,可以將url中的變量映射到功能處理方法的參數上,其中url 中的 {xxx} 占位符可以通過@pathvariable(“xxx“) 綁定到操作方法的入參中。
示例代碼:
1
2
3
4
5
6
7
|
@responsebody @requestmapping ( "/testurlpathparam/{param1}/{param2}" ) public void testurlpathparam(httpservletrequest request, @pathvariable string param1, @pathvariable string param2) { system.out.println( "通過pathvariable獲取的參數param1=" + param1); system.out.println( "通過pathvariable獲取的參數param2=" + param2); } |
postman發送請求截圖:
輸出結果:
通過pathvariable獲取的參數param1=1
通過pathvariable獲取的參數param2=2
2.@requestheader注解
@requestheader 注解,可以把request請求header部分的值綁定到方法的參數上。
示例代碼:
1
2
3
4
5
|
@responsebody @requestmapping ( "/testheaderparam" ) public void testheaderparam(httpservletrequest request, @requestheader string param1) { system.out.println( "通過requestheader獲取的參數param1=" + param1); } |
postman發送請求截圖:
輸出結果:
通過requestheader獲取的參數param1=abc
3.@cookievalue注解
@cookievalue 可以把request header中關于cookie的值綁定到方法的參數上。
示例代碼:
1
2
3
4
5
6
|
@responsebody @requestmapping ( "/testcookieparam" ) public void testcookieparam(httpservletrequest request, httpservletresponse response, @cookievalue string sessionid) { system.out.println( "通過cookievalue獲取的參數sessionid=" + sessionid); } |
postman發送請求截圖:
輸出結果:
通過cookievalue獲取的參數sessionid=ebef978eef6c46f8a95cc0990d2d360a
4.@requestparam注解
@requestparam注解用來處理content-type: 為 application/x-www-form-urlencoded編碼的內容。提交方式為get或post。(http協議中,form的enctype屬性為編碼方式,常用有兩種:application/x-www-form-urlencoded和multipart/form-data,默認為application/x-www-form-urlencoded);
@requestparam注解實質是將request.getparameter() 中的key-value參數map利用spring的轉化機制conversionservice配置,轉化成參數接收對象或字段,
get方式中querystring的值,和post方式中body data的值都會被servlet接受到并轉化到request.getparameter()參數集中,所以@requestparam可以獲取的到;
該注解有三個屬性: value、required、defaultvalue; value用來指定要傳入值的id名稱,required用來指示參數是否必錄,defaultvalue表示參數不傳時候的默認值。
示例代碼:
1
2
3
4
5
6
|
@responsebody @requestmapping ( "/testrequestparam" ) public void testrequestparam(httpservletrequest request, @requestparam (value = "num" , required = true , defaultvalue = "0" ) int num) { system.out.println( "通過requestparam獲取的參數num=" + num); } |
postman發送請求截圖:
輸出結果:
通過requestparam獲取的參數num=10
5.@requestbody注解
@requestbody注解用來處理httpentity(請求體)傳遞過來的數據,一般用來處理非content-type: application/x-www-form-urlencoded編碼格式的數據;
get請求中,因為沒有httpentity,所以@requestbody并不適用;
post請求中,通過httpentity傳遞的參數,必須要在請求頭中聲明數據的類型content-type,springmvc通過使用handleradapter配置的httpmessageconverters來解析httpentity中的數據,然后綁定到相應的bean上。
示例代碼:
1
2
3
4
5
|
@responsebody @requestmapping ( "/testrequestbody" ) public void testrequestbody(httpservletrequest request, @requestbody string bodystr){ system.out.println( "通過requestbody獲取的參數bodystr=" + bodystr); } |
postman發送請求截圖:
代碼運行結果:
通過requestbody獲取的參數bodystr=這是body的內容
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/haha12/p/10336363.html