Last active
December 19, 2015 13:09
-
-
Save kubamarchwicki/5959892 to your computer and use it in GitHub Desktop.
Validating method parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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