Java property validation

1. Validation validation:

There are two types of Settings: (1) directly check the parameter properties (such as @requestParam in front of), add verification annotations in front of the parameter

(2) To verify the parameter object (for example, @requestBody), add @VALID before the parameter object

Then add validation annotations to properties in the object

2. Usage details:

As shown in the figure, use the corresponding verification annotation, and set the message(error message) in it, can return the corresponding error message. Common verification annotation can be seen in Demo

Attention!! If you want the attribute to have a mandatory value, you must add @notnull; If the Max check of numeric type is required and the value is less than or equal to 1, the value is as follows:

If @notnull is not added, only the passed value is verified. If this attribute is not passed, no verification is performed. In this case, this attribute is configurable

3. Set an exception return

If the check fails, return MethodArgumentNotValidException abnormalities, can set @ ExceptionHandler at this time

demo.java


package com.gfss.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;
import java.math.BigDecimal;
import java.util.Date;

public class Demo {
    @Null(message = "Must be null")
    String name1;

    @NotNull(message = "Cannot be null")
    String name2;

    @NotEmpty(message = "Non-empty string")
    String notEmptyname;

    @Range(message = "Range, must be numeric type, Max is maximum, min is minimum",max = 10 , min = 1)
    Integer range;

    @Pattern(regexp = "", message = "Regular expression, must be a string, the value of regexp is regular expression.")
    String pattern;


    @NotNull
    @Size(min = 2,max = 10,message = "String length size")
    String a;

    @NotNull
    @Max(message = "The maximum, which is less than or equal to value, can be a string.",value = 1)
    Integer max;

    @Min(message = "Minimum, that is, greater than or equal to value, can be a string.",value = 1)
    Integer min;

    @Past(message = "Must be a past date, must be a time type.")
    Date pastdate;

    @Future(message = "Must be a future date, must be a time type.")
    Date futuredate;

    @AssertTrue(message = "Must be true")
    Boolean assertTrue;

    @AssertFalse(message = "Must be false")
    Boolean assertFalse;

    @Email(message = "Must be in E-mail format")
    String mail;

    @DecimalMax(value = "21.01", message = "Decimal Max, is the value of value, value is a string")
    BigDecimal decimalMax;

    @DecimalMax(value = "21.01", message = "Decimal Max, is the value of value, value is a string")
    BigDecimal decimalMin;
}
Copy the code

Pay special attention to

  • @notempty for collection classes
  • @notblank is used on strings
  • @notnull is used for primitive types
  • @Pattern can only be used with strings