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!

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top