Pergunta

The question is very short:

Let's consider there is the following class:

public class someClass implements someInterface {
    @NotNull
    String someString;

    // ... some cool logic, getters, setters, etc.
}

And this interface:

public interface SomeInterface {
    // cool stuff
}

Somewhere else is this method:

public <T extends SomeInterface> T doStuff (T someInterface) {
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<T>> violatons = validator.validate();
    // ...
    // ...
    // ...
    return someInterface;
}

I don't have much experience with BV. Will someString in SomeClass be validated or do I need to cast someInterface to SomeClass explicitly?

The scenario here is simplified. In reality I have different generic Classes and interfaces and it would mean a little bit of work. That's why I'm asking carefully.

Hope there's one of those experts hangin around!

Thanks in advance!

Foi útil?

Solução

Bean Validation will obtain the constraints from the actual type of the validated instance, i.e. you can pass a variable of the interface type and Bean Validation will consider the constraints from the implementation (constraints declared on super-types and interfaces apply as well).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top