【问题标题】:Duplicate annotation - hibernate validator重复注释 - 休眠验证器
【发布时间】:2012-07-23 15:29:13
【问题描述】:

我想在一个属性上多次使用一个 hiberante 验证器注释。

例如:

@Length(min = 10, max = 10, groups = {Lenght10Group.class})
@Length(min = 5, max = 5, groups = {Lenght5Group.class})
private String productCode;

我找到了以下线程 Multiple annotations of the same type on one element? 并尝试将其应用于我的应用程序。

我创建了 Lengths.java 文件:

public @interface Lenghts {
   Lenght[] value();
}

并通过以下方式应用它:

@Lenghts( {@Length(min = 10, max = 10, groups = {Lenght10Group.class}), 
           @Length(min = 5, max = 5, groups = {Lenght5Group.class}) })
private String productCode;

但它似乎不起作用。

我错过了什么?是否需要自定义验证器,自定义处理程序?谁能帮我解决这个问题?我会很感激。

【问题讨论】:

    标签: java hibernate validation annotations hibernate-validator


    【解决方案1】:

    您应该使用开箱即用的 Hibernate Validator 提供的 @Length.List。所有内置约束(由 BV 规范定义和由 Hibernate Validator 定义的附加自定义约束)都定义了这样的 @List 注释。

    【讨论】:

    • 听起来不错!我明天试试,如果有帮助,我会告诉你。
    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多