Reputation: 63
I'm using the validation in Java Jersey, and i'm building an application where it should be possible to save a draft of something. But before it is published, i would like it to be valid, using the annotations in the view model. When publishing, the same savemethod, for draft, is invoked. Therefor i cannot use the annotation in the method (methodName(@Valid ViewmodelName)). Can i somehow invoke a method to validate the annotations in the viewModel? Something like model->isValid(). Alternatively i could make two viewmodels; one for draft and one for published, but seems kind of double work.
Best regards
Upvotes: 0
Views: 888
Reputation: 6466
so from your comment I think what you want is to manually validate your annotated bean. This is fairly easy. What you need to do is:
I created a small sample that illustrates that:
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
public class HibernaterTest {
public static void main(String[] args) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
MyModel m = new MyModel();
m.test = null;
Set<ConstraintViolation<MyModel>> validate = validator.validate(m);
System.err.println(validate.size());
m.test = "some string";
validate = validator.validate(m);
System.err.println(validate.size());
}
public static class MyModel {
@NotNull
String test;
}
}
The first validation call will print 1, while the second will print 0. This is because the model's test property originally is null.
I hope this answers your question,
Artur
Upvotes: 1