How to validate attributes in Spring Batch

Asked

Viewed 216 times

1

I’m working with Spring Batch. In my batch processing, my Reader needs to read a file .csv.

When I read the file, I have a class that represents each line, and I would like to know how to validate the input data. I know that the Spring uses some tags like @NotNull, @NotEmpty, but I couldn’t make it work with Spring Batch, there are several examples of using these tag’s, but along with Spring MVC.

1 answer

0


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

  • Are you using Maven in the project dependents? Bean Validation is just a specification and so needs an implementation like Hibernate Validator.

  • 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);

  • 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.

  • 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

  • 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.

  • You can create an Exception to do this, adjusted my answer to such.

  • 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.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.