How to use Hibernate Validator Engine Relocation Artifact in the Java Library
The method of using Hibernate Validator Engine Relocation Artifact in the Java Library
Overview:
Hibernate Validator is an open source Java class library to implement the Java Bean verification specification (JSR 380).It provides a set of strong verification annotations and APIs that can easily verify the Java objects.Hibernate Validator's positioning is a method that can provide better flexibility and scalability when using Hibernate Validator.
step:
The following is the steps of using Hibernate Validator ENGINE RELOCATION Artifact in the Java library:
1. Before starting, make sure that the Hibernate Validator has been introduced in the project.
2. Open your Java library project and find the pom.xml file.
3. Add the following dependencies to pom.xml, and replace the version number to the latest version:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-engine</artifactId>
<version>6.1.7.Final</version>
</dependency>
4. Add the following plug -in configuration to pom.xml to generate the repositioned version of the Hibernate Validator engine:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>org.hibernate.validator</pattern>
<shadedPattern>[YOUR_PACKAGE].hibernate.validator</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Make sure to replace the `YOUR_PACKAGE]` `` ``
5. Run the `MVN Package` command to build a project.
6. After the construction is completed, the generated jar file will contain a resolved Hibernate Validator engine.
7. Using generated jar files, you can use Hibernate Validator annotation and API as before.
Example code:
Below is an example code that uses Hibernate Validator Engine Relocation Artifact:
import org.hibernate.validator.HibernateValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
public class Main {
public static class User {
@javax.validation.constraints.Email
private String email;
@javax.validation.constraints.NotEmpty
private String password;
// Getters and setters...
}
public static void main(String[] args) {
Validator validator = Validation
.byProvider(HibernateValidator.class)
.configure()
.buildValidatorFactory()
.getValidator();
User user = new User();
user.setEmail("invalidEmail");
user.setPassword("");
Set<ConstraintViolation<User>> violations = validator.validate(user);
for (ConstraintViolation<User> violation : violations) {
System.out.println(violation.getPropertyPath() + " " + violation.getMessage());
}
}
}
The above example demonstrates how to use Hibernate Validator for basic attribute verification.You can add more verification annotations and business logic as needed.
Summarize:
By following the above steps, the Hibernate Validator Engine Relocation Artifact is used in the Java class library. You can achieve better flexibility and scalability, and use the powerful verification function provided by Hibernate Validator to protect your Java object.