Objective:
Some friends on the Internet shared the use of hibernate-validator, but not the information about the associated libraries, but the lack of custom classes in the reference code provided.
I hope this blog will allow you to smoothly run out of the expected results.
If there is any mistake, you can leave me a message.
Good friends in English can refer to the official website of getting started.
http://hibernate.org/validator/documentation/getting-started/
First, the environment
The Hibernate-validator library must be running with a JDK version of 1.6 and above.
Second, Hibernate-validator Library and dependence
1classmate-1.3.1. Jar2hibernate-validator-5.3.1. Final.jar3javax.el-2.2.4. Jar4javax.el-api-2.2.4. Jar5jboss-logging-3.3.0. Final.jar6validation-api-1.1.0. Final.jar
Third, suppose we construct a car class
1 Public classCar {2 3 @NotNull4 PrivateString manufacturer;5 6 @NotNull7@Size (min =2, max = -)8 PrivateString licenseplate;9 Ten@Min (2) One Private intSeatcount; A - PublicCar (string manufacturer, String licenceplate,intSeatcount) { - This. Manufacturer =manufacturer; the This. licenseplate =licenceplate; - This. Seatcount =Seatcount; - } - + PublicString Getmanufacturer () { - returnmanufacturer; + } A at Public voidSetmanufacturer (String manufacturer) { - This. Manufacturer =manufacturer; - } - - PublicString getlicenseplate () { - returnlicenseplate; in } - to Public voidsetlicenseplate (String licenseplate) { + This. licenseplate =licenseplate; - } the * Public intGetseatcount () { $ returnSeatcount;Panax Notoginseng } - the Public voidSetseatcount (intSeatcount) { + This. Seatcount =Seatcount; A } the +}
Four, how to check it? Let's take a look at this test class
1 Public classCartest {2 3 Public Static voidMain (string[] args) {4Validatorfactory factory =validation.builddefaultvalidatorfactory ();5Validator Validator =factory.getvalidator ();6Car car =NewCar (NULL,"Sue A999999",1);7 8Set<constraintviolation<car>> constraintviolations =validator.validate (car);9 for(constraintviolation<car>constraintviolation:constraintviolations)TenSystem. out. println ("Error:"+constraintviolation.getmessage ()); One } A}
V. What is the result?
1 , . Ten:afternoon org.hibernate.validator . internal. util. Version <clinit>25.3. 1 . Final3 error: cannot be null4 Error: Minimum cannot be less than 2
Six, really simple to use! Although the library repeatedly "invented" the wheel.
Seven, have you noticed? The result is an automatic internationalization!
Hibernate Validator introduced in non-web projects