I have a scenario where I am validating a List as NotNull for a method getTest see below example.
Using Hibernate Validator 4.3.0 Final
<pre>
public interface IApplication {
void getTest(@NotNull List<String> someList);
}
=============================
@AutoValidator
//This is similar to the MethodValidation Interceptor and using guice as DI
@Slf4j
public class ApplicationImpl implements IApplication {
@Override
public void getTest(List<String> app) {
log.debug("Entered the method");
}
}
====================================
public class ValidationInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
MethodValidator validator = validatorFactory.getValidator().unwrap(
MethodValidator.class);
//validate the all parameter values if they have been annotated with any constraints
Set<MethodConstraintViolation<Object>> violations = validator
.validateAllParameters(
invocation.getThis(), invocation.getMethod(),
invocation.getArguments());
if (!violations.isEmpty()) {
throw new MethodConstraintViolationException(violations);
}
Object result = invocation.proceed();
//validate the return values if they have been annotated with any constraints
violations = validator.validateReturnValue(
invocation.getThis(),
invocation.getMethod(),
result);
if (!violations.isEmpty()) { throw new MethodConstraintViolationException(violations); } }
return result;
}
}
==================================
Unit test
public class AppTest {
@Test
public void getTest(){
List<String> app = null;
appSvc.getTest(app);
}
}
==================================
</pre>
Any help would be appreciated.
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
I have a scenario where I am validating a List as NotNull for a method getTest see below example.
Using Hibernate Validator 4.3.0 Final
<pre>
public interface IApplication { void getTest(@NotNull List<String> someList); }
=============================
@AutoValidator
//This is similar to the MethodValidation Interceptor and using guice as DI
@Slf4j
public class ApplicationImpl implements IApplication {
@Override
public void getTest(List<String> app) { log.debug("Entered the method"); }
}
====================================
public class ValidationInterceptor implements MethodInterceptor {
@Inject
private ValidatorFactory validatorFactory;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
MethodValidator validator = validatorFactory.getValidator().unwrap(
MethodValidator.class);
//validate the all parameter values if they have been annotated with any constraints
Set<MethodConstraintViolation<Object>> violations = validator
.validateAllParameters(
invocation.getThis(), invocation.getMethod(),
invocation.getArguments());
if (!violations.isEmpty()) { throw new MethodConstraintViolationException(violations); }
Object result = invocation.proceed();
//validate the return values if they have been annotated with any constraints
violations = validator.validateReturnValue(
invocation.getThis(),
invocation.getMethod(),
result);
if (!violations.isEmpty()) { throw new MethodConstraintViolationException(violations); } }
return result;
}
}
==================================
Unit test
public class AppTest {
@Test
public void getTest(){ List<String> app = null; appSvc.getTest(app); }
}
==================================
</pre>
Any help would be appreciated.