Reputation: 5132
I am using javax.validation.constraints.NotNull in one of the POJO as follows:
public class Abc {
@NotNull
private final String x;
@NotNull
private final String y;
}
And then even when I make the object of Abc with null values for both the fields, it doesn't throw any exception. Any idea why?
Eg.
Abc abc = new Abc(null, null);
doesn't throw any exception.
Upvotes: 1
Views: 8159
Reputation: 36
For implementation that annotations Spring needs proxy of your class,so you can achieve exception behavior in case you inject Spring bean to some of your classes and call the method on the injected by Spring proxy,by that way the exception will be thrown and do not forget that you component should be annotated as @Validated
Upvotes: 0
Reputation: 187
Maybe this answer from a similar question helps you.
To activate parameter validation, simply annotate the class with @Validated
Upvotes: 0
Reputation: 77177
Annotations are just extra information attached to the items they annotate; they don't inherently have built-in logic. If you're using tooling like Lombok or the Kotlin language, the compiler may support automatically adding logic based on the annotations, but otherwise, they don't "do anything" until you actually make an active check (for example, by running your POJO through a validator).
Upvotes: 3