Java 用户定义的验证耦合问题

Java 用户定义的验证耦合问题,java,validation,bean-validation,Java,Validation,Bean Validation,定义客户端能够使用的API。客户端将通过各种方式连接。其中之一是java到java。在这种情况下,我遇到了一个问题。显然,API应该尽可能地与实现解耦。我还没有机会对此进行测试,但是用户定义的验证不会破坏这个模型吗 我正在通过API服务器端实现上的Spring@Validated启用验证。不想将其放入@Controller类,因为这不是进入服务(API)的唯一途径 例如,如果我在接口中定义了此方法: SomeObject updateOperation( AnInputClass param)

定义客户端能够使用的API。客户端将通过各种方式连接。其中之一是java到java。在这种情况下,我遇到了一个问题。显然,API应该尽可能地与实现解耦。我还没有机会对此进行测试,但是用户定义的验证不会破坏这个模型吗

我正在通过API服务器端实现上的Spring@Validated启用验证。不想将其放入@Controller类,因为这不是进入服务(API)的唯一途径

例如,如果我在接口中定义了此方法:

SomeObject updateOperation( AnInputClass param) ...
然后,我可以使用JSR-303验证进行注释,并且仍然可以进行解耦: @非空 SomeObject updateOperation(@NonNull AnInputClass参数)。。。 但是,如果我想对输入“param”的各个部分进行自定义验证,我需要创建自己的注释,,它有一个@Constraint(validatedBy)部分,该部分将与验证实现相关联。这个的缩写形式如下所示:

SomeObject updateOperation ( @CheckInput AnInputClass param)... 
...where the annotation is defined as
...
@Constraint(validatedBy = CheckInputValidator.class)   // this is the coupling issue
public @interface CheckInput { ....
因为这一切都发生在服务器端,所以不需要让Java客户机拥有这个CheckInputValidator类;然而,我看不到任何选择。首先,我喜欢在API中进行验证——它们告诉用户将要验证什么。如果我能打破依赖关系,将验证转移到实现上,这似乎是一个可以接受的折衷方案。然而,这导致了下面的异常,所以我似乎被卡住了。有人能帮忙吗

javax.validation.ConstraintDeclarationException: Only the root method of an 
overridden method in an inheritance hierarchy may be annotated with parameter 
constraints, but there are parameter constraints defined at all of the 
following overridden methods

我自己找到了答案,我早该意识到这一点

我所需要做的就是在接口/API层中使用“@Valid”注释。然后,确保用户定义/自定义注释上的@Target注释已定义“TYPE”,将@CheckInput注释应用于所需的类,所有操作都会正常工作