Joke Collection Website - Talk about mood - Customized validation annotations: Solve the problem of dynamic multi-field linkage verification

Customized validation annotations: Solve the problem of dynamic multi-field linkage verification

javax.validation is a set of interfaces defined based on the JSR-303 standard. The purpose is to allow developers to verify parameters concisely. hibernate-validator implements this set of interfaces and can be referenced independently as a tool.

If it is a SpringBoot project, there is no need to explicitly reference the above dependencies, because SpringBoot has already integrated the above dependencies.

Now we assume that a scenario order adds two new fields, type1 and type2. These two fields affect the judgment of the orderId value range. That is to say, the orderId range judgment is no longer static, but is affected by Other fields are affected.

For this situation, in the first step, we can construct the type1, type2, orderId combined fields, and in the second step, we can customize the validator to separate the combined fields for business verification.

The first chapter of this article introduces the basic concept of validation, the second chapter introduces the basic application of validation, and the third chapter introduces dynamic verification of fields through custom annotations. I hope this article will be helpful to everyone. .