You can use the Validation Bean following the example:
Class with the annotations:
public class Cliente {
@NotNull("O nome é obrigatorio")
@Size(min = 3, max = 20)
private String nome;
@NotNull("O sobre nome é obrigatorio")
@Size(min = 3, max = 40)
private String sobrenome;
// getters e setters
}
Code to validate annotations:
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Cliente cliente = new Cliente();
cliente.setNome("Ana");
cliente.setSobrenome("S.");
Set<ConstraintViolation<Cliente>> restricoes = validator.validate(cliente);
if(!restricoes.isEmpty()){
//EDIT
trow new MyCustomConstraintViolationException(restricoes);
}
//EDIT
Class Mycustomconstraintviolationexception:
public class MyCustomConstraintViolationException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Set<ConstraintViolation<Cliente>> restricoes
public MyCustomConstraintViolationException(Set<ConstraintViolation<Cliente>> restricoes) {
super("");
this.restricoes = restricoes;
}
public Set<ConstraintViolation<Cliente>> getRestricoes(){
return restricoes;
}
}
Henrique, I did as you put it, but an error in Factory.getValidator(); The error is as follows: javax.validation.Validationexception: HV000183: Unable to load 'javax.el.Expressionfactory'. Check that you have the EL dependencies on the classpath, or use Parametermessageinterpolator Instead
– Douglas Trigo
Are you using Maven in the project dependents? Bean Validation is just a specification and so needs an implementation like Hibernate Validator.
– Henrique Luiz
I’m using yes. I researched and added an extra dependency. <dependency> <groupid>javax.el</groupid> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency> But now the problem to work is that the Validator.validate(client) method does not only receive one class, is the signature of the method: <T> Set<Constraintviolation<T>> validate(T Object, Class<? >... groups);
– Douglas Trigo
I am using Hibernate Validator. I added the dependency cited in the comment above and stopped giving error. Now the problem is this second parameter that the validate method has.
– Douglas Trigo
Groups can be omitted, follow the documentation of Hibernate Validator Example 2.9. . https://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-usingvalidator.html#d0e565
– Henrique Luiz
Henry, it worked, he accepts only one parameter, I managed to catch the return of the restrictions. You know if there’s any other way to do this validation. Maybe something like popping an error if the class is not filled in the way I want? Maybe in my Itemprocessor. I would like something like this because I would like to capture the error in the Processor System and then treat the error message.
– Douglas Trigo
You can create an Exception to do this, adjusted my answer to such.
– Henrique Luiz
Henry, thank you so much for the answer. I implemented it in my code and it worked. I’m just not gonna put it as the right answer to see if anyone else suggested another strategy. I was wanting to use something like @Valid in the method to not have to instantiate Validator.
– Douglas Trigo