Request parameter processing in spring MVC

Posted by usefulphp on Mon, 03 Jan 2022 23:26:34 +0100

Main knowledge points

  • The servlet API gets the request parameters using request getParameter("xxx")
  • In spring MVC, you only need to declare the parameter with the corresponding name in the processing method to automatically match the parameter passed in by the request and perform the corresponding type conversion (the servlet API also needs to judge the type by itself)
  • If the type does not match / cannot match, a 400 error will be reported
  • Matching rules
    • The request parameter must be consistent with the parameter name of the processing method
    • null is assigned if the parameter of the processing method is not passed in
    • If the parameter name passed in is inconsistent with the parameter name of the processing method, it can be matched through @ RequestParam("xxx")

Processing request parameter garbled


On tomcat's server Add URIEncoding="utf-8" to the XML file

<Connector port="8080" protocol="HTTP/1.1"
               redirectPort="8443" URIEncoding="utf-8" />


Configure the encoding filter characterencoding filter to solve Chinese garbled code
In spring MVC's web Add the configuration of garbled code processing interceptor in XML

<!--        solve post Response garbled code-->
<!--        Turn on the encoding setting of request and response at the same time-->

<!--        Interception rules(/* (intercept all)-->
<!--        <servlet-name>according to servlet Name interception</servlet-name>-->

@RequestParam gets the parameters of the request

    public String params01(@RequestParam(value = "xxx",defaultValue = "aqin") String name){
        return "/index.jsp";
  • value rename parameter
  • required specifies whether the parameter must be passed in
    • true by default (no 400 error is reported)
    • false can be passed (null is automatically passed in) and no error will be reported
      • So pay attention! When declaring the passed in parameters of a function, do not use the underlying data type, because the underlying data type cannot receive null
  • defaultValue is the default value. When the passed in parameter is null, a default value will be automatically set (required=false can be omitted for this parameter)

@RequestHeader get request header information

Gets the value of the Host field in the header

	@RequestMapping(value = "/getheader",params = {"username"})
    public String params02(@RequestHeader("Host") String host){
        return "/index.jsp";

@Cookie value gets the value in the cookie

Gets a cookie with a specified key value

@RequestMapping(value = "/getcookie",params = {"username"})
    public String params02(@CookieValue("JSESSIONID") String cookie){
        return "/index.jsp";

Get all cookie s

@RequestMapping(value = "/getcookie",params = {"username"})
    public String params03(@CookieValue Map<String,String> cookies){
        return "/index.jsp";


@RequestMapping(value = "/heheda",method = {RequestMethod.POST})
    public String params02(){
        return "index.jsp";

From spring 4 3 begins with a set of notes on the simplified request mode:

  • @PostMapping("heheda")
    • Here @ RequestMapping(value = "/heheda",method = {RequestMethod.POST}) is equivalent to @ PostMapping("/heheda")
  • @GetMapping()
  • @PutMapping()
  • @DeleteMapping()

params: set request parameters

  1. Some parameters must be params = {username}
  2. Some parameters params={"!username"}
  3. The parameter must be equal to a value params={"username=aqin"}
  4. The parameter must not be equal to a value params={"username!=aqin"}

headers: set the request header

  1. Must contain a value headers = {"accept language = zh CN, Zh; q = 0.9"}

Consumers: set the content type of the current request

  1. The content type of the current request must be the specified value
  2. Frequently requested content types:
    1. application/x-www-form-urlencoded form the default content type submitted by the form
    2. Content type of multipart / form data form submission file stream
    3. json content type submitted by application/json ajax
  3. Request content type mismatch: HTTP Status 415
  4. consumes={"application/x-www-form-urlencoded"}


Mapped URLs can also support wildcards

  • ? One? Can match a single character (a-z0-9)[1]
  • *A * can match any character (a-z0-9) [any]
  • *** * match any character, any level
//http://localhost/aqin/user/123 Will match this
    @RequestMapping(value = "/user*")
    public String params05(){
        return "/index.jsp";

//http://localhost/aqin/user/1 Will match this
    @RequestMapping(value = "/user?")
    public String params06(){
        return "/index.jsp";

//http://localhost/aqin/1/2/3/user/ Will match this
    @RequestMapping(value = "/**/user")
    public String params07(){
        return "/index.jsp";

Note: if the mapping has an inclusion relationship, it will be handed over to the more accurate mapping first

No uniform >? > * >**


Parameter used to get URL directory level

    public String params04(@PathVariable("id") Integer id){
        return "/index.jsp";

When the request link is http://localhost:8080/aqin/user/1012 The id in the function params04(@PathVariable("id") Integer id) will get the value of 1012.

Topics: Java Web Development Tomcat Spring MVC Annotation