This article brings you a detailed explanation of how Spring receives web request parameters in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1 Query parameters
Request format: url?Parameter 1=value 1&Parameter 2=value 2...
Applies to both For GET and POST methods
There are several ways to write query parameters in spring:
Method one:
The method parameter name is the request parameter name
// 查询参数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; }
Method two:
Extract parameters from 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; }
Method three:
The method parameter name and the request parameter name can be different, and the parameters are bound through the @RequestParam annotation
// 查询参数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; }
Method four:
Create an entity class object as the parameter carrier. Spring will automatically bind the parameters to the properties of the entity class object based on the parameter name.
// 查询参数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; }
The entity class is defined as follows:
@Data @NoArgsConstructor @AllArgsConstructor @Builderpublic class User { private String username; private String password; }
Used here With the third-party library lombok, there is no need to manually add get, set and other methods in the code, lombok will be added automatically.
The curl command to send the request is as follows:
curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'
The interactive message is as follows:
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 Form parameters
The request parameters are not in the url, but in the Body. The format is: url? Parameter 1=value 1¶meter 2=value 2...
Applicable to POST method
The form parameter processing method is almost exactly the same as the previous request parameter processing method, except that the method method is set to the POST method in the RequestMethod annotation
Method one:
// 表单参数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; }
Method two:
// 表单参数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; }
Method three:
// 表单参数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; }
Method four:
// 表单参数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; }
The curl request command is as follows:
curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1
The request and response message is as follows:
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 Path parameter
The request parameter is part of the url, the format is: url/parameter 1/parameter 2...
Applicable to both GET and POST methods
The code is as follows:
@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; }
The request curl command is as follows:
curl -i http://192.168.1.14:8080/test/url/aaa/bbb
The request and response message are as follows:
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 format parameters
The request parameters are in the Body and are in json format. Need to add request header: Content-Type: application/json;charset=UTF-8
Applicable to POST method
Method 1:
Define entity class, parse json object into strength class, need to add RequestBody annotation
// 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; }
Method 2:
If you don’t want to define an entity class, you can also parse the json request directly into the JSONObject class
// 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; }
Method 3:
Also The json object can be parsed directly into a Map object
// 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; }
The request curl command is as follows:
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
The request and response message is as follows:
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
The above is the detailed content of Java explains in detail how Spring receives web request parameters. For more information, please follow other related articles on the PHP Chinese website!