The value of @ RequestParam receive parameter is the processing mode of null

  • 2021-12-11 07:21:55
  • OfStack

Directory @ RequestParam receive parameter value is null for @ RequestParam 1 small question 1 question 2

The @ RequestParam receive parameter has a value of null


@RequestMapping(value = "/test")
 public String test( @RequestParam(value = "profit",required = false,defaultValue = "0") int profit){
      System.out.println("profit:"+profit);
       return "success";
}

The first processing method (as shown above): defaultValue request parameter default value, 1 and required = false 1 use

The second processing mode: If the received parameter is null, int should be changed to Integer, and the default value of Integer is null


@RequestMapping(value = "/test")
 public String test(@RequestParam(
 @RequestParam(value = "profit",required = false) Integer profit){
      System.out.println("profit:"+profit);
       return "success";
}

A few questions about @ RequestParam

Question 1

First of all, I used @ RequestParam to check whether the parameter was null when I was using spring, but I think I had some misunderstanding about the usage of @ RequestParam.

Simply put, @ RequestParam can only verify whether you passed this parameter, but not whether the parameter you passed is empty.

Question 2

There is another problem. Once I wrote a code as follows: method (@ RequestParam User user), and then reported the following error: Required ES50parameter 'xxx' is not present

Just delete @ RequestParam


Related articles: