Last active
May 24, 2018 20:52
-
-
Save fmbenhassine/11014a29252c7c3ee88a to your computer and use it in GitHub Desktop.
Bean Validation sample #lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javaee.beanvalidation; | |
import javax.validation.constraints.NotNull; | |
public class Foo { | |
@NotNull(message = "{name.error}") | |
private String name; | |
public Foo() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javaee.beanvalidation; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.Validation; | |
import javax.validation.Validator; | |
import javax.validation.ValidatorFactory; | |
import java.util.Set; | |
public class Main { | |
public static void main(String[] args) { | |
Foo foo = new Foo(); | |
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | |
Validator validator = factory.getValidator(); | |
Set<ConstraintViolation<Foo>> constraintViolations = validator.validate(foo); | |
if (constraintViolations.size() > 0) { | |
for (ConstraintViolation<Foo> constraintViolation : constraintViolations) { | |
System.out.println(constraintViolation.getMessage()); | |
} | |
} else { | |
System.out.println("Foo instance is ok"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name.error=name is required |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment