Use Hibernate Validator to verify Bean Parameters

Source: Internet
Author: User
Tags in domain

Bean Validation class library comparison

Recently, when I was writing a project, I wanted to add the Bean Property Verification, just as there was a verification for each value in Domain in Grails. However, the workload for writing beautiful data by yourself is not small, and some people may have done this basic job. After checking on the Internet, we found the jsr303 standard, and there was a very mature implementation class library. After reading the circle, there are three better ones: Hibernate Validator, apache bval, and spring validator.

1. spring validator, A special feature is integration with spring mvc, which can be verified at the Controller layer.

2. apache bval, A large blank space. I think there are few people to use, and it is too challenging to get started.

3. hibernate Validator, the method used is also my favorite annotation. A closer look is that the verification is very different from the original one. It must produce a Validator object and then verify the Bean object. It is indeed more flexible to think about it.

Use of Hibernate Validator

According to the examples on the official website, I quickly ran through. But found a detailed problem, how to start running are reported that Log4j can not find the TRACK, and later found that the original log4j must use a log4j-1.2.16.jar or a later version.

I am also using very simple, just some basic data boundary verification, and the declared bean is very beautiful. To simplify the code, I simply wrapped Validator. Paste the code for your reference.

The extracted Bean code:

 
 
  1. Public class User {
  2. @ Email (message = "Email format Verification Failed ")
  3. @ Size (max = 32, message = "the mailbox length cannot exceed 32 characters ")
  4. Private String email; // the user's email address.
  5.  
  6. @ NotBlank (message = "nickname cannot be blank ")
  7. @ Size (min = 4, max = 16, message = "the length of the nickname must be between 4 and 16 characters ")
  8. Private String nickname; // nickname
  9. @ NotBlank (message = "the password cannot be blank ")
  10. @ Size (min = 6, max = 16, message = "the password length must be 6 to 16 characters ")
  11. Private String password; // logon password
  12. Public User validate (){
  13. ValidateUtil. validate (this );
  14. Return this;
  15. }
  16.  
  17. // Omitted later
  18. //......
  19.  
  20. }

Encapsulated Validator:

 
 
  1. Public class ValidateUtil {
  2. Private static Validator validator; // It is thread-safe
  3.  
  4. Static {
  5. ValidatorFactory factory = Validation. builddefavalivalidatorfactory ();
  6. Validator = factory. getValidator ();
  7. }
  8. Public static <T> void validate (T t ){
  9. Set <ConstraintViolation <T> constraintViolations = validator. validate (t );
  10. If (constraintViolations. size ()> 0 ){
  11. String validateError = "";
  12. For (ConstraintViolation <T> constraintViolation: constraintViolations ){
  13. ValidateError + = constraintViolation. getMessage () + ";";
  14. }
  15. Throw new ValidateException (validateError );
  16. }
  17. }
  18. }

 

Use of the Android Environment

I intended to use it in both java and android environments, but after a long time, I found that java. beans. introspector is a core class used by Hibernate Validator, but this class is not included in the android environment. After comparison, we found that many j2se classes in android have been cut down, which also explains why some jars cannot be run in the android environment.

Helpless solution:
1. the Validator 4.2.0 version is used. At least this version is not used by the Log class. It is slf4j. System verification is added to the called place. If it is a Dalvik environment, verification is not performed. Code example: if (System. getProperty ("java. vm. name"). equals ("Dalvik "))
2. after searching for the Internet for a long time, someone mentioned that it could be solved by repacking jarjar. After looking for some information, it was a little troublesome. If you have time, try it and paste it again.

 

References:
1. http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.