SpringBoot2.x series of tutorials (sixty) Java Bean Validation detailed explanation and international integration

For web projects, parameter verification of external interfaces is essential. If there are few interface parameters, you can also check one by one through ifelse, but if there are many parameters, writing code in this way will become very redundant.

As a programmer, abstract and unified processing ability is also an important indicator of programming ability. This article will take you to complete the unified processing of web project parameter verification based on Java Bean Validation.

Bean Validation

The JSR303 specification is a sub-specification in Java EE 6: Bean Validation. The official reference implementation is Hibernate Validator. JSR303 is used to validate the values ​​of fields in Java Beans. This article is also based on the implementation of Hibernate to complete the parameter verification.

Bean Validation defines the corresponding metadata model and API for JavaBean validation. The default metadata is Java Annotations, and the original metadata information can be overwritten and extended by using XML.

In the application, by using Bean Validation or custom constraints (constraint), such as @NotNull, @Max, @ZipCode, etc. to ensure the correctness of the data model (JavaBean).

Constraints can be attached to fields, getter methods, classes, or interfaces. For some specific needs, users can easily develop customized constraints. Bean Validation is a runtime data validation framework. After validation, the validation error message will be returned immediately.

Hibernate Validator provides the implementation of all the built-in constraints in the JSR303 specification, in addition to some additional constraints. Common annotations such as @Null, @NotNull, @Min(value), @Max(value), @Size(m

Guess you like

Origin blog.csdn.net/wo541075754/article/details/107227779