Discussion on whether @ RequestParam parameter must be passed

  • 2021-08-28 20:14:22
  • OfStack

1. Source code presentation


@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
 @AliasFor("name")
 String value() default "";
 @AliasFor("value")
 String name() default "";
 boolean required() default true; 
 String defaultValue() default ValueConstants.DEFAULT_NONE;
}

This is the sample code


@GetMapping("/findById")
  public ResultBean byid(@RequestParam(defaultValue = "1",required = false,/*value = "ss",*/name = "ls") Integer id){
    List<Customers> all = customersService.findById(id);
    return ResultBean.ok(all);
  }

Step 2: Description

2.1 @ RequestParam, for parameter entry, this annotation is unnecessary, but it can only match the name of the entry parameter defined by you, which is exactly the same. If it corresponds, it will match, and if it does not correspond, it will not match.

2.2 required, whether it is necessary, defaults to true, that is to say, when @ RequestParam exists, it is the default required = true condition, and when @ RequestParam does not exist, the explanation of 2.1 is returned.

2.3 defaultValue, set the initial value, 1 and required = false1. If no value is passed, the default value is used.

2.4 value, and name, like 1, give variables aliases, 11 corresponds to the attribute name passed by the front end, and maps to the back-end interface variable name. Sometimes the front end cannot pass you the variable name you want as required, so this is useful.

2.5 name, like value1, aliases variables. When both exist, an error occurs when accessing them.

Supplement: @ RequestParam of springMVC is a must drop

Look at the code first:


@RequestMapping(value = "/campaigns/{pageNo}/{pageSize}", method = RequestMethod.GET)
public String getList(@PathVariable("pageNo") int pageNo, 
  @PathVariable("pageSize") int pageSize,
  @RequestParam(defaultValue = "0") int status,
  @RequestParam(required=false) String keyword) {
    //....
    return null;
}

1.@RequestParam

In the above code, @ RequestParam is not added in 1 case, and the method parameters can obtain the corresponding bound data in 1 case, so many people think that @ RequestParam can be added or not added.

It was not until later learned that Java's reflection mechanism generated objects that did not hold parameters containing methods, that is, parameter names were not kept in the class file, so spring could not reflect bindings.

Then the question comes, why can we bind at ordinary times?

This is because, when Debug mode is turned on, it can be bound, while eclipse used in general is turned on by default.

Binding is possible in Debug mode because in project > Properties > Add variable attributes to generated class files (used by the debugger) is selected in Java Compiler.

You know, turning on Debug mode will affect performance, so when it is officially launched, it will be turned off. If we don't add it, we will report the following errors:

Request processing failed; nested exception is java. lang. IllegalArgumentException: Name for argument type...

2.@PathVariable

The @ PathVariable is used to specify the field to get the placeholder parameter in URL in @ RequestMapping, such as the pageNo field in @ PathVariable ("pageNo") int pageNo above to get the value of the placeholder {pageNo}.

The name in @ PathVariable parentheses must correspond to the placeholder name 1, and the field name can be different from 1.

If you don't specify a name in parentheses, you will also encounter the problem of 1 in non-debug mode

Therefore, the name in brackets in @ PathVariable is a must.


Related articles: