Skip to content

Instantly share code, notes, and snippets.

@kubamarchwicki
Last active December 19, 2015 13:09
Show Gist options
  • Save kubamarchwicki/5959892 to your computer and use it in GitHub Desktop.
Save kubamarchwicki/5959892 to your computer and use it in GitHub Desktop.
Validating method parameters
public class SomeBean {
@Interceptors(ValidationInterceptor.class)
public void addAuthor(@Size(min=5) String name,
String surename) {
Author a = new Author();
a.setName(name);
a.setSurename(surename);
em.persist(a);
}
}
public class ValidationInterceptor {
@Inject
private ValidatorFactory validatorFactory;
@AroundInvoke
public Object validateMethodInvocation
(InvocationContext ctx) throws Exception {
MethodValidator validator = validatorFactory.getValidator()
.unwrap(MethodValidator.class);
Set<MethodConstraintViolation<Object>> violations = validator.validateAllParameters(
ctx.getTarget(),
ctx.getMethod(),
ctx.getParameters());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment