Constraints validation for user inputs (javax.validation.constraints)
UPDATED: 24 February 2018
Tags:
Hibernate
,
J2SE
,
spring
,
spring-boot
,
validation
User input validation is common part of any application. JBoss developers made it easy to validate inputs using Java Bean Validation framework.
Bean Validation 2.0 (http://beanvalidation.org, JSR 380)
We will be using Bean Validation framework 2.0 for the example. Its certified implementation is Hibernate Validator 6.0.1.Final or above. Framework 2.0 require Java 8 or higher version.
Source code (User.java)
Source code (ValidatorFactoryExample.java)
Output
Other frameworks like Spring triggers validation using annotation so you don't have to validate bean on your own like we did using ValidatorFactory.
Similar
Passing and validating RequestParam in spring-boot rest api
Bean Validation 2.0 (http://beanvalidation.org, JSR 380)
We will be using Bean Validation framework 2.0 for the example. Its certified implementation is Hibernate Validator 6.0.1.Final or above. Framework 2.0 require Java 8 or higher version.
Source code (User.java)
import javax.validation.constraints.Email; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Size; /** * @author javaQuery * @since 2018-02-24 * @github: https://github.com/javaquery/Examples */ public class User { @NotEmpty(message = "firstName can not be empty") @Size(min = 2, max = 20, message = "firstName length must be between 2 and 20") private String firstName; @NotEmpty(message = "lastName can not be empty") @Size(min = 2, max = 20, message = "lastName length must be between 2 and 20") private String lastName; @NotEmpty(message = "nickNames can not be empty") private List<@Size(min = 2, message = "nickName length must be greater than 2") String> nickNames; @Email private String email; @NotEmpty(message = "password can not be empty") @Size(min = 6, message = "password length must be at least 6 character") private String password; // getter-setter }
- @NotEmpty - We are using it to check string can not be empty. Can be used with collection, map or array.
- @Size - We are using it to check the length of string. Can be used with collection, map or array.
- @Email - The string has to be a well-formed email address.
There are other annotations like @NotNull, @Min, @Max, @Past, @Future, @PastOrPresent, @FutureOrPresent, etc... Complete list of annotations is available on Bean Validation specification.
Source code (ValidatorFactoryExample.java)
import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; /** * @author javaQuery * @since 2018-02-24 * @github: https://github.com/javaquery/Examples */ public class ValidatorFactoryExample { public static void main(String[] args) { User user = new User(); user.setFirstName("Vicky"); user.setEmail("not-an-email"); user.setNickNames(Arrays.asList("vicks", "v")); user.setPassword("1234567"); ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); /* validate object and get constraints failed */ Set<ConstraintViolation<User>> violations = validator.validate(user); for (ConstraintViolation<User> violation : violations) { System.err.println(violation.getMessage()); } } }
Output
nick name lenght must be greater than 2 lastName can not be empty must be a well-formed email address
Other frameworks like Spring triggers validation using annotation so you don't have to validate bean on your own like we did using ValidatorFactory.
Similar
Passing and validating RequestParam in spring-boot rest api
Tags:
Hibernate
,
J2SE
,
spring
,
spring-boot
,
validation
0 comments :