本篇文章帶給大家的內容是java詳解Spring接收web請求參數的方式 。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。
1 查詢參數
請求格式:url?參數1=值1&參數2=值2...
同時適用於GET和POST方式
spring處理查詢參數的方法又有幾種寫法:
方法一:
方法參數名即為請求參數名稱
// 查询参数1 @RequestMapping(value = "/test/query1", method = RequestMethod.GET) public String testQuery1(String username, String password) { System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法二:
從HttpServletRequest中提取參數
// 查询参数2 @RequestMapping(value = "/test/query2", method = RequestMethod.GET) public String testQuery2(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法三:
方法參數名和請求參數名可以不一樣,透過@RequestParam註解來綁定參數
// 查询参数3 @RequestMapping(value = "/test/query3", method = RequestMethod.GET) public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) { System.out.println("username=" + un + ", password=" + pw); return "username=" + un + ", password=" + pw; }
方法四:
建立一個實體類別物件作為參數承載載體,spring會根據參數名稱自動將參數綁定到實體類別物件的屬性上
// 查询参数4 @RequestMapping(value = "/test/query4", method = RequestMethod.GET) public String testQuery4(User user) { String username = user.getUsername(); String password = user.getPassword(); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
實體類別定義如下:
@Data @NoArgsConstructor @AllArgsConstructor @Builderpublic class User { private String username; private String password; }
這裡用到了第三方函式庫lombok,這樣就不需要在程式碼中手動加入get、set等方法,lombok會自動加入。
傳送請求的curl指令如下:
curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'
互動封包如下:
GET /test/query1?username=aaa&password=bbb HTTP/1.1 Host: 192.168.1.14:8080 User-Agent: curl/7.58.0 Accept: */*HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Thu, 25 Oct 2018 07:01:30 GMT username=aaa, password=bbb
2 表單參數
請求參數不在url中,而是在Body體中,格式為:url?參數1=值1&參數2=值2...
適用於POST方式
#表單參數處理方法和前面的請求參數處理方法幾乎完全一樣,只是RequestMethod註解中將method方法設定成POST方法
方法一:
// 表单参数1 @RequestMapping(value = "/test/form1", method = RequestMethod.POST) public String testForm1(String username, String password) { System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法二:
// 表单参数2 @RequestMapping(value = "/test/form2", method = RequestMethod.POST) public String testForm2(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法三:
// 表单参数3 @RequestMapping(value = "/test/form3", method = RequestMethod.POST) public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) { System.out.println("username=" + un + ", password=" + pw); return "username=" + un + ", password=" + pw; }
方法四:
// 表单参数4 @RequestMapping(value = "/test/form4", method = RequestMethod.POST) public String testForm4(User user) { String username = user.getUsername(); String password = user.getPassword(); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
curl請求指令如下:
curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1
請求與回應訊息如下:
POST /test/form1 HTTP/1.1 Host: 192.168.1.14:8080 User-Agent: curl/7.58.0 Accept: */* Content-Length: 25 Content-Type: application/x-www-form-urlencoded username=aaa&password=bbbHTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Thu, 25 Oct 2018 07:05:35 GMT username=aaa, password=bbb
3 路徑參數
請求參數為url中的一部分,格式為:url/參數1/參數2...
同時適用於GET和POST方式
程式碼如下:
@RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET) public String testUrl(@PathVariable String username, @PathVariable String password) { System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
請求curl指令如下:
curl -i http://192.168.1.14:8080/test/url/aaa/bbb
請求與回應封包如下:
GET /test/url/aaa/bbb HTTP/1.1 Host: 192.168.1.14:8080 User-Agent: curl/7.58.0 Accept: */*HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Thu, 25 Oct 2018 07:07:44 GMT username=aaa, password=bbb
4 json格式參數
請求參數在Body體中,並且為json格式。需要新增請求頭:Content-Type: application/json;charset=UTF-8
適用於POST方式
方法一:
定義實體類,將json物件解析成實力類,需要新增RequestBody註解
// json参数1 @RequestMapping(value = "/test/json1", method = RequestMethod.POST) public String testJson1(@RequestBody User user) { String username = user.getUsername(); String password = user.getPassword(); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法二:
如果不像定義實體類,也可以將json請求直接解析成JSONObject類別
// json参数2 @RequestMapping(value = "/test/json2", method = RequestMethod.POST) public String testJson2(@RequestBody JSONObject json) { String username = json.getString("username"); String password = json.getString("password"); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
方法三:
也可以將json物件直接解析成Map物件
// json参数3 @RequestMapping(value = "/test/json3", method = RequestMethod.POST) public String testJson3(@RequestBody Map<String, String> userMap) { String username = userMap.get("username"); String password = userMap.get("password"); System.out.println("username=" + username + ", password=" + password); return "username=" + username + ", password=" + password; }
請求curl指令如下:
curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '{ "username" : "aaa", "password" : "bbb" } 'http://192.168.1.14:8080/test/json1
請求與回應封包如下:
POST /test/json1 HTTP/1.1 Host: 192.168.1.14:8080 User-Agent: curl/7.58.0 Accept: */* Content-Type: application/json;charset=UTF-8 Content-Length: 52 { "username" : "aaa", "password" : "bbb" }HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Thu, 25 Oct 2018 07:09:06 GMT username=aaa, password=bbb
以上是java詳解Spring接收web請求參數的方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!