How to use JSR 303 (Bean) validation?

I read a lot of manuals about the JSR 303 specification, but I don't see any example ready for release. Everywhere is described how to get an object Set<Constraintviolation<T>>.

Example:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
Set<ConstraintViolation<Car>> violations = validator.validate(car);

But what's next? I want to inform the handler (client) of the method that this method parameter is in an inconsistent state.

What should I do with Set<ConstraintViolation<Car>>? Do I need to manually iterate over Set<ConstraintViolation>, collecting all error messages on a single line, and then throw an exception with these messages?

Or are there even more convenient ways out of the box?

Or is it better to provide a validatemethod inside each bean?

+5
source share
2 answers

- . , . , bean <bean> is not valid .

, , api. .

, , . , , . , - , . - . , ConstraintViolationException, .

+1

, Bean Validation. - API Bean Validation . Set > . , . Bean Validation API Bean Validation. , JPA. JPA Bean Validation , (pre-persist/update/delete) javax.validation.ConstraintViolationException . JSF, Bean , . , Bean API , , JSR 303.

+1

All Articles