preface

JCP

JCP stands for Java Community Process (JCP).

The JCP is an open international technical standards organization responsible for developing and updating Java technical specifications. The 24-member executive Board is the organization’s highest decision-making body and is responsible for charting the course of Java’s technological evolution.

JSR

JSR stands for Java Specification Requests (JSR).

Is a formal request to the JCP 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.

Bean Validation

Bean Validation, as the name implies, validates Java beans. So far, Java validates beans in three specifications.

JSR-303 : Bean Validation

Jsr-303: Bean Validation is a Java specification proposal that was officially approved as a Java specification on November 16, 2009.

JSR 349: Bean Validation 1.1

Jsr-349 official website

JSR 349: Bean Validation 1.1 is the 1.1 version of Bean Validation that became a Java specification on May 24, 2013.

JSR 380: Bean Validation 2.0

Official website of jSR-380 specification

JSR 380: Bean Validation 2.0 is the 2.0 version of Bean Validation, which became a Java specification on August 21, 2017.

javax.validation

Javax. Validation is an implementation of the Bean Validation specification provided by Java. It provides @notBlank, @notnull, @notempty annotations for us to use.

Maven coordinates are as follows

< the dependency > < groupId > javax.mail. Validation < / groupId > < artifactId > validation - API < / artifactId > < version > 2.0.1. The Final < / version > </dependency>Copy the code

Because Bean Validation has multiple versions, javax. Validation also provides an implementation for that version, as shown in the figure

Among them

  • X corresponds to JSR-303: Bean Validation
  • X corresponds to JSR 349: Bean Validation 1.1
  • X corresponds to JSR 380: Bean Validation 2.0

Hibernate-Validator

Hibernate-Validator is Hibernate’s implementation of the JSR-303: Bean Validation specification. It is based on Javax. Validation and provides @range, @Length, and other annotations on top of it.

Spring parameter verification

Parameter validation is the most fundamental requirement of Web development, and Spring naturally supports it. The Hibernate-Validator framework in Spring MVC is used to validate Java Bean type parameters.

Hibernate Validator doesn’t matter because parameters can’t be verified if they’re not Java beans, so Spring has added support for normal parameter types.

Therefore, when using the Spring framework for parameter verification, multi-party techniques are used. This is why annotations for parameter validation come from Javax, Hibernate, and Spring.

Validates parameters of Java bean types

See Using Hibernate-Validator for Parameter validation of Java bean types

Verify parameters of common types

Validation of common types is the spring framework’s complement to hibernate-Validator validation of Java beans only. Generally used when there are few arguments (there is no need to wrap a Java Bean for one or two arguments).

In this case, we can directly add a validation annotation to the corresponding parameter, and then add an @validated annotation to the Controller. This annotation is the spring validation annotation for common type parameters.

Examples are as follows

Exception handling of Spring parameter verification

There must be some cases where the parameter verification fails. If the verification fails, an exception will be thrown. Although Spring handles these exceptions by default, the default handling method is not safe.

Please refer to how to handle these exceptions

Exception handling of Spring parameter verification

reference

Why validator