The writer works for a small company or a government project. So when you come in and take over the code, it’s a long story. Initially all the code was a single data model to receive front-end data. The result is that each fetch requires a block of code to verify that the parameter is valid. I couldn’t take it anymore. I did some searching. Found the @valid annotation. There is no unified model. You don’t have to judge them individually. The following is the demo of the big guys.

The following example

1: First you need to add annotations to the corresponding field of the entity class to act as a verification condition, such as @max, as follows (age is an attribute in the User class)

@Max(value = 18,message = "Too old")
    private Integer age;
Copy the code

2: Secondly, add @valid annotation to the parameter to be verified in the method of the Controller layer, and pass in the BindingResult object to obtain feedback information in the case of verification failure.

@PostMapping("/users")
    public User addUser(@RequestBody @Valid User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return null;
        }
        return userResposity.save(user);
    }
Copy the code

BindingResult. GetFieldError. GetDefaultMessage () is used to obtain the corresponding field to add the contents of the message, such as: @ Max annotations in the content of the message property

There are other validation rules as well

limit instructions
@Null The limit can only be NULL
@NotNull The limit must not be NULL
@AssertFalse The limit must be false
@AssertTrue The restriction must be true
@DecimalMax(value) The limit must be a number not greater than the specified value
@DecimalMin(value) The limit must be a number not less than the specified value
@Digits(integer,fraction) The limit must be one decimal, and the number of digits in the integer part cannot exceed integer, and the number of digits in the decimal part cannot exceed fraction
@Future The limit must be a future date
@Max(value) The limit must be a number not greater than the specified value
@Min(value) The limit must be a number not less than the specified value
@Past The restriction must be a past date
@Pattern(value) Restrictions must conform to 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 is not empty (string length is not 0, collection size is not 0)
@NotBlank Verify that the element value of the annotation is not null (not null, length 0 after removing the first whitespace), unlike @notempty, where @notBlank applies only to strings and removes whitespace from strings when comparing
@Email Verify that the element value of the annotation is Email, or you can specify a custom Email format using regular expressions and flag

Custom annotations are for another time. Ready for bed.