注解@notBlank的工作原理
程序员文章站
2024-01-07 20:08:52
...
Prerequisite
add the following dependency in pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
I declare message attribute as NotBlank:
@NotBlank annotation is a “composite” annotation which is based on @NotNull:
And use the following code to explicitly trigger a validation:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.setMessage(null);
obj.printMessage();
// http://*.com/questions/24386771/javax-validation-validationexception-hv000183-unable-to-load-javax-el-express
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();
Set<ConstraintViolation<HelloWorld>> set = validator.validate(obj);
for (ConstraintViolation<HelloWorld> constraintViolation : set) {
System.out.println(constraintViolation.getMessage());
}
}
output:
A list of all available validator annotation
You can find a list of all available validator and their implementation from this folder:
C:\Users\i042416.m2\repository\org\hibernate\hibernate-validator\5.2.1.Final\hibernate-validator-5.2.1.Final\org\hibernate\validator\internal\constraintvalidators\bv
Debug - how does it work?
要获取更多Jerry的原创文章,请关注公众号"汪子熙":