How to integrate the Kie Commons Validation framework (Integration of Kie Commons Validation Framework in Java Class Libraries)
Integrating the Kie Commons Validation framework in the Java library is a simple and effective method for verifying and verifying the legitimacy of data.Kie Commons Validation is a rule -based verification framework that helps developers define and perform verification rules by regular engines.
The following will introduce how to integrate the Kie Commons Validation framework in the Java class library.
1. First, make sure to add the dependency item of the Kie Commons Validation framework to the project construction tool.You can add the following code fragments to the pom.xml file of the project:
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>7.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>7.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>7.0.0.Final</version>
</dependency>
2. Create a Java class to define verification rules.You can use the rules with annotations to define the verification rules, such as:
@Valid
public class Person {
@NotNull
@Size(min = 2, max = 50)
private String name;
@Min(18)
@Max(100)
private int age;
// Getters and Setters
}
3. Where to verify the data, create a Kiesession object and load the verification rules.Kiesession is a session of a rule engine that can perform verification rules.For example:
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieContainer.newKieSession();
Kiesession.insert (Person); // Insert the object to be verified into the session
int valueErrs = kiesession.fireallrules (); // execute verification rules
4. Judgment verification results.After the session of the rules engine is completed, it can be determined whether there is a verification failure by checking the error count of verification.For example:
if (validationErrors > 0) {
// Verification fails, handling errors
} else {
// Verification Pass and deal with normal situation
}
Through the above steps, you can integrate the Kie Commons Validation framework into the Java library, and use the rules engine to perform data verification and verification.
I hope this article will help you integrate Kie Commons Validation framework in the Java library.If necessary, you can customize the definition and verification results according to your specific needs.