如何在带有javax.validation且没有@Validate注释的方法中加入Validate
我正在用Java和spring Framework开发一个服务,该服务在方法中使用javax.validation注释,如以下示例:如何在带有javax.validation且没有@Validate注释的方法中加入Validate,java,spring,validation,Java,Spring,Validation,我正在用Java和spring Framework开发一个服务,该服务在方法中使用javax.validation注释,如以下示例: public void method( @NotNull @Positive Integer val1, @NotNull @PastOrPresent Date startDate, @NotNull @PastOrPresent Date endDate); 当我在类中编写注释@Validated时,它工作得很好,但是我在某些类中
public void method(
@NotNull @Positive Integer val1,
@NotNull @PastOrPresent Date startDate,
@NotNull @PastOrPresent Date endDate);
当我在类中编写注释@Validated时,它工作得很好,但是我在某些类中遇到了循环依赖的问题,注释@Validated产生了不满意的PendencyException错误。我将无法更改为@Lazy,但尽管如此,我必须使用javax.validation注释进行验证
因此,我想通过编程来验证方法,但是我还没有找到方法来验证方法,我只有在使用Dto类时才能从类中验证,方法验证为Validated类
有人能告诉我怎么做吗
问候
更新日期:2019年9月11日:
我通过以下方法找到了一种方法:
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.getEnclosuringMethod