In web development, there will be some code writing related to data validity verification.
Commonly written as:
public class User { public String username; public String password; public Integer age; }
public addUser(User user) {
String username = user.getUsername();
Integer age = user.getAge();
String password = user.getPassword();
String errorMsg = "";
if(username == null || username.equals("") {
errotMsg += "Username cannot be empty";
}
// ..........A lot of if, how many to write, too mechanical. Just a code porter.
}
Java background verification related issues:
javax.validation.Validator
org.hibernate.validator.internal.engine.ValidatorImpl
1. User adds comments
public class User { @NotNull public String username; @NotNull public String password; @NotNull public Integer age; }
public addUser(User user) { Iterator<ConstraintViolation<Object>> iterator = validator.validate(user) StringBuilder buf = new StringBuilder(256); while (iterator.hasNext()) { buf.append(";"); ConstraintViolation<?> elem = iterator.next(); if (elem != null) { buf.append(elem.getPropertyPath() + elem.getMessage()); } } System.out.println(buf); }
utils to use it.
public class Validators { private static class ValidatorHolder { static Validator validator = new Validators().getValidator(); } private final Logger logger = LoggerFactory.getLogger(Validators.class); private final ValidatorFactory factory; private Validators() { try { Configuration<?> factoryCfg = Validation.byDefaultProvider().configure(); factory = factoryCfg.buildValidatorFactory(); } catch (final ValidationException ex) { logger.error("Bean Validation provider can not be found, no validation will be performed"); throw ex; } } private Validator getValidator() { return factory.getValidator(); } /** * Verify the value of a single property of the specified bean * @param bean The bean that needs to be verified * @param name attribute name to be checked * @return the error message of the property that needs to be checked */ public static String validateProperty(Object bean, String name) { return parseConstraintViolations(ValidatorHolder.validator.validateProperty(bean, name)); } /** * Check all properties of the specified bean * @param bean The bean that needs to be verified * @return all error messages */ public static String validate(Object bean) { return parseConstraintViolations(ValidatorHolder.validator.validate(bean)); } public static String parseConstraintViolations(Set<ConstraintViolation<Object>> constraintViolations) { if (CollectionUtils.isEmpty(constraintViolations)) { return null; } Iterator<ConstraintViolation<Object>> iterator = constraintViolations.iterator(); ConstraintViolation<Object> first = iterator.next(); //Single check error if (!iterator.hasNext()) { return first.getPropertyPath() + first.getMessage(); } // two or more elements StringBuilder buf = new StringBuilder(256); //Java default is 16, probably too small if (first != null) { buf.append(first.getPropertyPath() + first.getMessage()); } while (iterator.hasNext()) { buf.append(";"); ConstraintViolation<?> elem = iterator.next(); if (elem != null) { buf.append(elem.getPropertyPath() + elem.getMessage()); } } return buf.toString(); } }