@Valid verification

@Valid annotation for verification, the packet belongs to: javax.validation.Valid.

① First, on the need to add the appropriate fields in the entity class acts as a check for the annotation conditions, such as: @min, the following code (age of a User class attributes):

 

@Min (value = 18, message = " Age invalid")
Private Integer Age;
② Then add annotations @Valid parameters to be verified on the method layer controller, and need to pass BindingResult object for acquiring verification feedback information in case of failure, the following code:

 

 

@PostMapping ( "/ Users")
public the addUser the User (User @RequestBody @Valid the User, the BindingResult BindingResult) {
IF (bindingResult.hasErrors ()) {
System.out.println (bindingResult.getFieldError () getDefaultMessage ().);
Return null;
}
return userResposity.save (User);
}
bindingResult.getFieldError.getDefaultMessage () message for acquiring the respective contents on the added fields, such as: @Min annotation content of the message attribute
-------- --------
Disclaimer: this article is the original article CSDN bloggers "xzmeasy", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/xzmeasy/article/details/76098188

Guess you like

Origin www.cnblogs.com/muxi0407/p/11607093.html