【问题标题】:How to dynamically update the argument of OOTB hibernate validator annotations?如何动态更新 OOTB 休眠验证器注释的参数?
【发布时间】:2015-05-19 18:51:13
【问题描述】:

我使用@Min 注解进行字段验证。

@Min(100)
private Long cost;

我想将注解的参数提取到单独的配置文件中。

有办法实现吗?

附言

我知道我可以编写自己的注释和验证器,但我想重用库的代码。

【问题讨论】:

  • mmm ...所以您想将动态值传递给编译时间参数,对吗?由于java在实际的类文件中删除了注解相关的代码,你需要在编译时从配置文件中添加参数,所以我认为唯一的方法是编写你自己的注解。

标签: java validation annotations bean-validation hibernate-validator


【解决方案1】:

使用@Min、@Max 等注释无法做到这一点。您可以使用@AssertTrue 来注释您的实体的方法,您自己的验证逻辑将在其中实现。

public class MyEntity {
     private Long cost;

     //getters and setters...
     @AssertTrue
     public boolean isValid() {
          long minCost = MyExternalConfig.getMinCost(); //get data from where you want
          return cost > minCost; 
     }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多