The use and difference of @RequestBody, @RequestParam, @PathVariable and @Vaild annotations




@RequestBody

@RequestBody is mainly used to receive the data in the json string passed from the front end to the back end (data in the request body);


The Get method has no request body, so when using @RequestBody to receive data, the front end cannot use the Get method to submit the data;
instead, it uses the Post method to submit the data. In the same receiving method of the backend, @RequestBody and @RequestParam() can be used at the same time; @RequestBody can have at most one, while @RequestParam() can have multiple.


  • Note: A request has only one RequestBody; a request can have multiple RequestParams.

注:当同时使用 @RequestParam()和 @RequestBody时:

	@RequestParam()指定的参数可以是普通元素、 数组、集合、对象等等
	
	(即当:@RequestBody 与 @RequestParam() 可以同时使用时,原 SpringMVC 接收参数的机制不变,
	只不过 RequestBody 接收的是请求体里面的数据;
    而 RequestParam 接收的是 key-value 里面的参数,所以它会被切面进行处理从而可以用普通元素、
    数组、集合、对象等接收)
    
    即:如果参数时放在请求体中,传入后台的话,那么后台要用 @RequestBody 才能接收到;如果不是放
    在请求体中的话,那么后台接收前台传过来的参数时,要用 @RequestParam 来接收,或则形参前什么
    也不写也能接收。

注:如果参数前写了 @RequestParam(xxx),那么前端必须有对应的 xxx 名字才行
   (不管其是否有值,当然可以通过设置该注解的 required 属性来调节是否必须传)
   如果没有 xxx 名的话,那么请求会出错,报400。

注:如果参数前不写 @RequestParam(xxx) 的话,那么就前端可以有可以没有对应的 xxx 名字才行,
    如果有 xxx 名的话,那么就会自动匹配;没有的话,请求也能正确发送。



@RequestParam

@RequestParam is used to map the data in the request parameter area to the parameters of the control layer function processing method


  • Main parameter syntax:

    @RequestParam(value="parameter name", required="true/false", defaultValue="")


1. value: the name of the parameter passed in the request, if you do not set the value value of the background interface, it will default to the variable name. If the first parameter in the figure below does not set value="pageNum", the parameter name passed in by the front end must be pageNum, otherwise the pageNum will not receive the corresponding data in the background interface.

2. required: Whether to include this parameter, the default is true, indicating that the request path must include this parameter, if not included, a 404 error will be reported. If it is set to false, when there is no such parameter in the request, it will default to null, and for variables of basic data types, there must be a value, and a null pointer exception will be thrown. If null values ​​are allowed, variables in the interface need to be declared using wrapper classes.

3. defaultValue: The default parameter value. If this value is set, required=true will be invalid, and it will be false automatically. If this parameter is not passed, the default value will be used.

Guyu




@PathVariable

Receive the value of the placeholder in the request path,
the @PathVariable annotation can bind the placeholder parameter in the URL to the input parameter of the controller processing method; the {xxx} placeholder in the URL can be passed @PathVariable("xxx" ) is bound to the input parameter of the action method.
As shown below:

Guyu





@Valid

The @Valid annotation can realize data verification. You can define the entity first, add validation rules to the attributes of the entity, and
add the @valid keyword when the Api receives data. At this time, the defined entity will enable a validation function.

When the @Valid annotation is used for validation, the package it belongs to is: javax.validation.Valid.


  • First, you need to add annotations to serve as verification conditions on the corresponding fields of the entity class, as shown below:

Guyu


  • Secondly, add the @Valid annotation to the parameters of the method verification of the controller layer, and need to pass in the Dto entity class object to verify and obtain the content of the message added to the corresponding field, as shown below:

insert image description here


  • Note: Don't misuse the exception type, for example @size is not available on int



----------- @difference comparison-----------




The difference between @RequestBody annotation and @RequestParam annotation

In Get requests, @RequestBody cannot be used. In Post request, @RequestBody and @RequestParam can be used, but if @RequestBody is used, the configuration of parameter conversion must be unified.

The parameters received by the @RequestParam annotation come from requestHeader, that is, the request header. Both are used to obtain the dynamic parameters in the request path url. That is, in the url, the format is xxx?username=123&password=456.
The parameters received by the @RequestBody annotation come from requestBody, that is, the request body.



The difference between @RequestParam annotation and @PathVariable annotation

The @RequestParam and @PathVariable annotations are used to receive requests from the request. Both can receive parameters. The key difference is that @RequestParam takes the value from the request, while @PathVariable is filled from a url template.

The @RequestParam annotation is to obtain the parameters passed in by the static url.
@PathVariable is to obtain the variables in the request path as parameters and needs to be used in conjunction with @RequestMapping("item/{itemId}").




----------- @verification information form -----------




Verification Information Instructions Sheet

limit illustrate
@Null Restriction can only be null
@NotNull limit must not be null
@AssertFalse limit must be false
@AssertTrue limit must be true
@DecimalMax(value) limit must be a number no greater than the specified value
@DecimalMin(value) limit must be a number not less than the specified value
@Digits(integer,fraction) The limit must be a decimal number, and the number of digits in the integer part cannot exceed integer, and the number of digits in the fractional part cannot exceed fraction
@Future Constraint must be a future date
@Max(value) limit must be a number no greater than the specified value
@Min(value) limit must be a number not less than the specified value
@Past Restriction must be a date in the past
@Pattern(value) Restrictions must match the specified regular expression
@Size(max,min) The limit character length must be between min and max
@Past Verify that the element value (date type) of the annotation is earlier than the current time
@NotEmpty Verify that the element value of the annotation is not null and not empty (the length of the string is not 0, the size of the collection is not 0)
@NotBlank Verify that the element value of the annotation is not empty (not null, the length is 0 after removing the first space), unlike @NotEmpty, @NotBlank is only applied to strings and the spaces of strings will be removed when comparing
@Email Verify that the element value of the annotation is Email, and you can also specify a custom email format through regular expressions and flags











Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2020 10 - Guyu.com | 【Copyright All Rights Reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/109311246#comments_27850914