Jersey/JAX-RS : How to cascade beans-validation recursively with @Valid automatically?

Actually, according to the specification, adding @Valid is exactly for this usecase. From the JSR 303 specification: In addition to supporting instance validation, validation of graphs of object is also supported. The result of a graph validation is returned as a unified set of constraint violations. Consider the situation where bean X contains a field … Read more

JSR 303 Bean Validation – Why on getter and not setter?

Annotating getters doesn’t mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply. The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type’s public API that way. … Read more

Spring MVC – @Valid on list of beans in REST service

@Valid is a JSR-303 annotation and JSR-303 applies to validation on JavaBeans. A java.util.List is not a JavaBean (according to the official description of a JavaBean), hence it cannot be validated directly using a JSR-303 compliant validator. This is supported by two observations. Section 3.1.3 of the JSR-303 Specification says that: In addition to supporting … Read more

Hibernate Validation of Collections of Primitives

Neither JSR-303 nor Hibernate Validator has any ready-made constraint that can validate each elements of Collection. One possible solution to address this issue is to create a custom @ValidCollection constraint and corresponding validator implementation ValidCollectionValidator. To validate each element of collection we need an instance of Validator inside ValidCollectionValidator; and to get such instance we … Read more

Initializing Spring bean from static method from another Class?

The factory-method should only contain the method name, not including the class name. If you want to use a static factory, give the class of the factory(!) to the bean declaration, if you want to use an instance factory, give the factory-bean to the bean declaration, but don’t give both: The class of the created … Read more

Bean validation size of a List?

I created simple class: public class Mock { @Size(min=1, max=3) private List<String> strings; public List<String> getStrings() { return strings; } public void set(List<String> strings) { this.strings = strings; } } And test: Mock mock = new Mock(); mock.setStrings(Collections.emptyList()); final Set<ConstraintViolation<Mock>> violations1 = Validation.buildDefaultValidatorFactory().getValidator().validate(mock); assertFalse(violations1.isEmpty()); mock.setStrings(Arrays.asList(“A”, “B”, “C”, “D”)); final Set<ConstraintViolation<Mock>> violations2 = Validation.buildDefaultValidatorFactory().getValidator().validate(mock); assertFalse(violations2.isEmpty()); It … Read more