SpringMVC parameter binding simply binds the key/value data requested by the client to the parameter of the Controller method, which can then be used in the Controller
Here are five common annotations to illustrate how to bind parameters:
1. @ PathVariable annotation
@pathVariable is used to obtain the dynamic parameters in the request URL. Variables in the URL can be mapped to the parameters of the function processing method, where {XXX} placeholders in the URL can be bound to the input parameter of the operation method through @pathvariable (” XXX “).
Sample code:
@ResponseBody
@RequestMapping("/testUrlPathParam/{param1}/{param2}")
public void testUrlPathParam(HttpServletRequest request, @PathVariable String param1,
@PathVariable String param2) {
System.out.println("Param1 = obtained from PathVariable" + param1);
System.out.println("Param2 = obtained from PathVariable" + param2);
}
Copy the code
Postman sends request screenshot:
Output result:
Parameter param1=1 Parameter param2=2 obtained through PathVariable
2. @ RequestHeader annotation
The @requestheader annotation binds the values of the Request header to the parameters of the method.
Sample code:
@ResponseBody
@RequestMapping("/testHeaderParam")
public void testHeaderParam(HttpServletRequest request, @RequestHeader String param1) {
System.out.println("Param1 = obtained by RequestHeader" + param1);
}
Copy the code
Postman sends request screenshot:
Output result:
Parameter param1= ABC obtained by RequestHeader
3. @ CookieValue annotation
The @cookievalue can bind the cookie value of the Request header to the parameter of the method.
Sample code:
@ResponseBody
@RequestMapping("/testCookieParam")
public void testCookieParam(HttpServletRequest request, HttpServletResponse response,
@CookieValue String sessionid) {
System.out.println("Parameter sessionID = obtained by CookieValue" + sessionid);
}
Copy the code
Postman sends request screenshot:
Output result:
Through the sessionid = ebef978eef6c46f8a95cc0990d2d360a CookieValue access parameters
4. @ RequestParam annotation
The @requestParam annotation is used to handle content-Type: coded Content for Application/X-www-form-urlencoded Content. The submission mode can be GET or POST. In the Http protocol, the encType attribute of the form is the encoding mode, which is commonly used in two ways: Application/X-www-form-urlencoded and multipart/form-data, default is Application/X-www-form-urlencoded);
The @requestParam annotation converts request.getParameter () key-value Map into a parameter receiving object or field using Spring’s ConversionService configuration. The queryString value in get and the body data value in POST are accepted by the Servlet and converted to the Request.getParameter() parameter set, so @requestParam can get it.
The annotation has three attributes: Value, Required, and defaultValue; Value specifies the id name of the value to be passed, required indicates whether the parameter is required, and defaultValue indicates the defaultValue if the parameter is not passed.
Sample code:
@ResponseBody
@RequestMapping("/testRequestParam")
public void testRequestParam(HttpServletRequest request,
@RequestParam(value = "num", required = true, defaultValue = "0") int num) {
System.out.println(Num = num= num= num + num);
}
Copy the code
Postman sends request screenshot:
Output result:
The parameter num=10 is obtained from RequestParam
5. @ RequestBody annotation
The @requestbody annotation is used to handle data passed from HttpEntity (RequestBody). It is usually used to handle non-content-type: application/x-www-form-urlencoded data.
In a GET request, @requestBody does not apply because there is no HttpEntity;
In a POST request, the content-type of the data must be declared in the request header for the argument passed through HttpEntity. SpringMVC parses the data in an HttpEntity using HttpMessageConverters configured by HandlerAdapter, and then binds it to the appropriate bean.
Sample code:
@ResponseBody
@RequestMapping("/testRequestBody")
public void testRequestBody(HttpServletRequest request, @RequestBody String bodyStr){
System.out.println("BodyStr = obtained from RequestBody" + bodyStr);
}
Copy the code
Postman sends request screenshot:
Code running results:
The parameter bodyStr= obtained from RequestBody is the body content