简体   繁体   English

如何在运行时更改注释/ Hibernate验证规则?

[英]How can I change annotations/Hibernate validation rules at runtime?

If have a Java class with some fields I want to validate using Hibernate Validator. 如果我有一个带有某些字段的Java类,我想使用Hibernate Validator进行验证。 Now I want my users to be able to configure at runtime which validations take place. 现在我希望我的用户能够在运行时配置哪些验证。

For example: 例如:

public class MyPojo {
    ...

    @NotEmpty
    String void getMyField() {
        ... 
    }

    ...
}

Let's say I want to remove the NotEmpty check or replace it with Email or CreditCardNumber , how can I do it? 假设我要删除NotEmpty检查或将其替换为EmailCreditCardNumber ,我该怎么办? Is it even possible? 它甚至可能吗? I guess it comes down to changing annotations at runtime... 我想它归结为在运行时更改注释...

You can't do it normally. 你不能正常做到。

Here's what I've done to get more dynamic validations working via Hibernate Validator. 以下是我通过Hibernate Validator进行更多动态验证所做的工作。

  1. Extend the ClassValidator class. 扩展ClassValidator类。
  2. Override the getInvalidVaues(Object myObj) method. 重写getInvalidVaues(Object myObj)方法。 First, call super.getInvalidValues(myObj) , then add the hook to your customized validation. 首先,调用super.getInvalidValues(myObj) ,然后将钩子添加到自定义验证中。
  3. Instantiate your custom validator and call getInvalidValues to validate. 实例化您的自定义验证器并调用getInvalidValues进行验证。 Any hibernate annotated validations will kick off at this point, and your custom dynamic validations (anything not supported by annotations) will kick off as well. 此时,任何带有hibernate注释的验证都将启动,您的自定义动态验证(注释不支持的任何内容)也将启动。

Example: 例:

public class MyObjectValidator extends ClassValidator<MyObject>
{
    public MyObjectValidator()
    {
         super(MyObject.class);
    }

    public InvalidValue[] getInvalidValues(MyObject myObj)
    {
        List<InvalidValue> invalids = new ArrayList<InvalidValue>();
        invalids.addAll(Arrays.asList(super.getInvalidValues(myObj)));

        // add custom validations here
        invalids.addAll(validateDynamicStuff(myObj));

        InvalidValue[] results = new InvalidValue[invalids.size()];
        return invalids.toArray(results);
    }

    private List<InvalidValue> validateDynamicStuff(MyObject myObj)
    {
        // ... whatever validations you want ...
    }

}

So your custom validation code can contain logic like "Do this validation, if the user configured it, otherwise do that one", etc. You may or may not be able to leverage the same code that powers the hibernate validations, but either way, what you are doing is more involved that the 'normal' use case for hibernate validator. 因此,您的自定义验证代码可以包含类似“执行此验证,如果用户配置它,否则执行该操作”等逻辑,等等。您可能会也可能无法利用支持hibernate验证的相同代码,但无论如何,你正在做的事情更多地涉及到hibernate验证器的“正常”用例。

Actually it is possible in hibernate validator 4.1. 实际上它可以在hibernate验证器4.1中实现。 Just read the documentation about programatic constraint creation . 只需阅读有关编程约束创建的文档。

I don't think you'll be able to remove or change the annotation, it's part of the class definition. 我不认为你能够删除或更改注释,它是类定义的一部分。 You can build a new class, which is possible at runtime but a little involved. 您可以构建一个新类,这可以在运行时进行,但有一点涉及。 Hibernate may support programmatic access to the validations and allow you to override the annotation, I don't know the API that well. Hibernate可能支持以编程方式访问验证并允许您覆盖注释,我不太了解API。 Hibernate does a bit of runtime class building itself... that might be a good place to learn how to do it if you're interested. Hibernate做了一些运行时类构建本身...如果你感兴趣的话,这可能是学习如何做的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM