SpringBoot 2 rapid integration | Hibernate Validator data validation

Outline

Submitted RESTFull API development and common forms are required to verify the data submitted by the user, such as: user name can not be empty, the age must be greater than 0, and so on. Here we are mainly talking about the background check, in SpringBoot we can check the background of the data by using Hibernate Validator.

Without further ado! Then we began to introduce how to use Hibernate Validator check to the data.

Hibernate Validator combat operations

We add functionality to transform the basis of users up.

User Model class code as follows:

public class User {
    private String name;
    private Integer age;
    //省略get and set 方法
}

Controller user code is as follows:
java @RequestMapping(value = "",method = RequestMethod.POST) public void add(User user){ log.info("添加用户成功:"+"name:{},age:{}",user.getName(),user.getAge()); }

The first step we add annotations to check at the User class, the specific code as follows:

public class User {
  @NotEmpty(message="请输入您的名称!")
  private String name;
  @Max(value = 100, message = "年龄必须在20-100之间!")
  @Min(value= 20 ,message= "年龄必须在20-100之间!" )
  private Integer age;
  ..省略get 和set 方法
}

Let's explain the meaning roughly annotation:

  • Annotation message: error message if the field of information and inspection rules do not meet the prompts.
  • The annotation value: indicates the field information defined value.
  • @NotEmpty: represents the variable name can not be empty.
  • @Max: it represents the maximum age is.
  • @Min: it represents the minimum age is.

The second step is for the user to add a method to transform

The method of adding the user to declare @Valid User parameter annotation, the annotation adding mainly instance to check specified parameters, then add users to add BindingResult process parameters.
BindingResult: mainly the result of the check data storage, we can be judged by BindingResult verify by checking and getting the error message.

Specific code as follows:
java @PostMapping() public User add(@Valid User user,BindingResult bindingResult){ if(bindingResult.hasErrors()) { String defaultMessage = bindingResult.getFieldError().getDefaultMessage(); System.out.println(defaultMessage); return null; } log.info("springboot添加用户成功:"+"name:{},age:{}",user.getName(),user.getAge()); return user; }

test:

Here it is Google tested using a plug-in tools: Advanced REST client to complete, you can choose your handy tool for testing.

Test case where the user name is empty:
image
background log output as follows:
image
the test data is not the age of 20 to 100:
image
Background log output as follows:
image

Hibernate Validator Details

Hibernate Validator is to achieve JSR349 validation framework, he has been defined to provide default check notes, we often use are as follows:

  • @NotNull: check field is empty
  • @NotEmpty: verifying that the set of map array element is empty and greater than 0
  • @Max (value =): defines the maximum finger
  • @Min (value =): defining a minimum field
  • @Size (min =, max =) : number of check range map element set
  • @Range (min =, max =) : parity or data interval range String Array
  • @Email: check whether the field is a mailbox
  • @Past: check whether an expiration date
  • @NotBlank: check whether the string that is non-empty! = null && equals ( "")
  • : @Valid class parameter declarations to verify the data

Guess you like

Origin www.cnblogs.com/jerry126/p/11531317.html