I have a bean that has a lot of fields annotated with JSR-303 validation annotations. There is a new requirement now that one of the fields is mandatory, but only in certain
just wanted to add more:
if you're using spring framework you can use org.springframework.validation.Validator
@Autowired
private Validator validator;
and to perform validation manually:
validator.validate(myObject, ValidationErrorsToException.getInstance());
and in controller:
@RequestMapping(method = RequestMethod.POST)
public Callable post(@RequestBody @Validated(MyObject.CustomGroup.class) MyObject request) {
// logic
}
although in this way extending from javax.validation.groups.Default
won't work so you have to include Default.class
in groups:
class MyObject {
@NotNull(groups = {Default.class, CustomGroup.class})
private String id;
public interface CustomGroup extends Default {}
}