我正在用Java和spring框架开发服务,该服务在方法中使用 javax.validation 注释,例如以下示例:
public void method(
@NotNull @Positive Integer val1,
@NotNull @PastOrPresent Date startDate,
@NotNull @PastOrPresent Date endDate);
当我在类中编写注释@Validated时,它可以正常工作,但是某些类StackOverFlow中的循环依赖项存在一些问题,注释@Validated产生 UnsatisfiedDependencyException 错误。 (尽管我必须使用javax.validation批注进行验证,但我无法更改为@Lazy)。
因此,我想以编程方式验证方法,但是我还没有找到对方法进行验证的方法,我只能使用方法 validate <从类中(当我使用Dto类时) / em>的有效类。
有人可以告诉我该怎么做。
致谢。
更新11/09/2019:
我从以下方法中找到了一种方法:
private <T> void validate(T currentObject, Method currentMethod, Object... parameterValues) {
Set<ConstraintViolation<T>> errors=null;
ExecutableValidator executableValidator = validator.forExecutables();
errors = executableValidator.validateParameters(currentObject, currentMethod, parameterValues);
System.out.println((errors != null && !errors.isEmpty())?"There are exceptions":"There are not exceptions");
}
如果有人可以轻松地进行思考,我将非常感谢您。
使用该方法时,可以在第一个和第二个参数中插入: this 和(new Object(){})。getClass()。getEnclosingMethod()。