Valdr Bean Validation framework and Java -class library integration guide
Valdr Bean Validation framework and Java -class library integration guide
Introduction:
Bean Validation is a specification of the Java Enterprise Edition (Java EE), which is used to verify the data integrity of the JavaBean object.It provides a declarative verification mechanism that allows developers to define verification rules through annotations, thereby simplifying the process of data verification.Valdr is a data verification framework for front -end. It uses JSON SCHEMA to define rules and perform verification in front -end applications.
In this guide, we will introduce how to integrate the Valdr Bean Validation framework into the Java class library.
1. Add dependencies:
First, you need to add a Valdr Bean Validation framework to Maven or Gradle to build a file.For example, in Maven, you can add the following code to your pom.xml file:
<dependency>
<groupId>org.hokkaido</groupId>
<artifactId>valdr-bean-validation</artifactId>
<version>1.2.0</version>
</dependency>
2. Create verification rules:
Next, you need to create verification rules for the JavaBean object.Valdr Bean Validation framework uses Java annotations to define these rules.The following is an example:
public class User {
@Notnull (Message = "Name cannot be empty")
@Size (min = 2, max = 20, message = "Name length must be between 2 and 20 characters")
private String name;
@Email (Message = "Email format is incorrect")
private String email;
// getters and setters
}
In the above example, we use two different verification annotations: @Notnull and @size.@Notnull is used to check whether the attribute is null,@siZe to check whether the length of the string attribute is within the specified range.You can use other verification annotations as needed.
3. Integrated Valdr framework:
To integrate the Valdr Bean Validation framework into your Java class library, you need to execute the following steps:
-Colon a java class and implement the `javax.validation.spi.validationProvider` interface.
- 在该类中,重写`javax.validation.spi.ValidationProvider#getValidationProviderResolver()`方法并返回一个`javax.validation.ValidationProviderResolver`对象。
-In the implementation of `javax.validation.ValidationProviderResolver`, use the` org.valdr.javaxvalidation.validationProviderResolver` class.
-In the implementation of `javax.validation.ValidationProviderResolver`, the constructor parameter setting of` org.valdr.javaxvalidation.validationProviderRoviderRoviderRoviderRoviderRovider is set to `new val. DrjsonjacksonvalidationProvider () `.
The following is an example implementation:
public class ValdrValidationProvider implements ValidationProvider<Configuration> {
@Override
public Configuration createSpecializedConfiguration(BootstrapState bootstrapState) {
return new ValdrConfigurationImpl(Validation.byDefaultProvider().configure());
}
@Override
public Configuration createGenericConfiguration(BootstrapState bootstrapState) {
return createSpecializedConfiguration(bootstrapState);
}
@Override
public ValidationProviderResolver getValidationProviderResolver() {
return new ValdrValidationProviderResolver();
}
}
4. Add configuration file:
Create a file called Validation.xml under the class path, and add the following:
<validation-config xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration
http://xmlns.jcp.org/xml/ns/validation/configuration/validation-configuration-2.0.xsd">
<default-provider>org.valdr.javaxvalidation.ValdrValidationProvider</default-provider>
</validation-config>
This configuration file tells the Java EE server with ValdRvalidationProvider when starting.
5. Add json schema file:
Create a file called Valdr.json under the class path, and write the JSON SCHEMA verification rules.For example:
json
{
"User": {
"properties": {
"name": {
"size": {
"min": 2,
"max": 20,
"MESSAGE": "The name length must be between 2 and 20 characters"
},
"notNull": {
"Message": "Name cannot be empty"
}
},
"email": {
"email": {
"MESSAGE": "Email format is incorrect"
}
}
}
}
}
In the above example, we created the verification rules for the USER class.
Summarize:
Through the above steps, we successfully integrated the Valdr Bean Validation framework into the Java class library.Now, you can use the JSON SCHEMA rule in the front -end application to verify the data returned by the back end.
Please note that the example code and configuration provided by this guide may need to be adjusted according to your application requirements.You can learn more about the official documentation of the Valdr Bean Validation framework to obtain more information about configuration and use.