Data validation

Data validation is a feature used by any application, whether it is the display layer or the persistence layer. Often, the same validation logic is spread across layers, wasting time and leading to errors. To avoid duplication, developers often write the validation logic directly into the domain model, but this confuses the domain model code with the validation logic, which should be the metadata describing the domain model.

Data verification can ensure the correctness of data.

JSR

JSR stands for Java Specification Requests, which stands for Java Specification proposals. Is a formal request to the JCP(Java Community Process) to add a standardized technical specification. Anyone can submit a JSR to add new apis and services to the Java platform. JSR has become an important standard in the Java world.

JSR303

JSR303 is a standard specification that Java provides for Bean data Validation, called Bean Validation. In the December 2009 release of Java EE6, Bean Validation was included as an important feature for validating field values in Java beans. The official reference implementation is Hibernate Validator.

JSR303 website jcp.org/en/jsr/deta…

Hibernate Validator website www.hibernate.org/subprojects…

Bean Validation defines the corresponding metadata type and API for JavaBean Validation. In the application, the validation rules are specified by annotating the Bean properties with standard annotations like @notnull, @max, and so on, and the Bean is validated through the validation interface of the annotations. Bean Validation is a run-time data Validation framework, and Validation errors are returned immediately after Validation.

JSR303 comments:

annotations function
@Notnull Verify that the object is not muL, cannot check the string of length 0, to verify the basic data type
@Null Verify that the object is null
@AssertTrue Verify that the Boolean object is true
@AssertFalse Verify that the Boolean object is false
@Max(value) Checks whether Number and String are less than or equal to the specified value
@Min(value) Checks whether Number and String are greater than or equal to the specified value
@DecimalMax(value) The annotated value must not be greater than the maximum specified in the constraint. The argument to this constraint is a string representation of the maximum value defined by Big Decimal, with Decimal precision
@DecimalMin(value) The annotated value must not be less than the minimum value specified in the constraint. The argument to this constraint is a string representation of a minimum value defined by Big Decimal, with Decimal precision
@Digits (integer, fraction) Validates if the string is a number in the specified format, Interger specifies integer precision and fraction specifies decimal precision
@Size(min, max) Verifies that the object (Array, Collection, Map, String) length is within the given range
@Past Verifies that Date and Calendar objects precede the current time
@Pattern Verify that a String complies with regular expression rules
@NotBlank Check whether the constraint string is Null and whether the trimmed length is greater than 0. Strings only, and whitespace is removed
@URL Verify that the URL is valid
@Email Verify that the email address is valid
@CreditCardNumber Verify that it is a valid credit card number
@Length(min, max) The length of the validation string must be within the specified range
@NotEmpty Check whether the element is NULL or EMPTY. Used for Array, Collection, Map, and String
@range (min, Max, message) The value of the validation property must be within the appropriate range

How to use JSR303 validation

Let’s make a comment here so that name only supports Email

Component / / registered bean
@ConfigurationProperties(prefix = "person")
@Validated  // Enable data verification
public class Person {
    @email (message=" wrong format ") // Name must be in mailbox format and message is a custom error message
    private String name;
}
Copy the code

Yaml file, the following name is not the mailbox format, will be executed later must be error

person:
  name: Wan Li 
  age: 3
  happy: true
  birthday: 2021/ 4/25
  maps: {k1: v1.k2: v2}
  lists:
    - code
    - music
    - game
  dog:
    name: Liu's dog
    age: 3
Copy the code

Run the

@SpringBootTest
class Springboot02ConfigApplicationTests {
    @Autowired
    private Person person;
    @Test
    void contextLoads(a) {
        System.out.println(person);
    }
Copy the code