【问题标题】:JSR 303 - Name of the annotated field in the custom validatorJSR 303 - 自定义验证器中注释字段的名称
【发布时间】:2017-11-16 16:12:06
【问题描述】:

我有一个关于自定义注释的问题。更准确地说,我是否可以访问自定义验证器中带注释的字段的名称? 如果是,我该如何访问?

谢谢

【问题讨论】:

    标签: spring bean-validation jsr


    【解决方案1】:

    你可以通过以下exp访问路径

    ((ConstraintValidatorContextImpl) context).getConstraintViolationCreationContexts().get(0).getPath()
    

    【讨论】:

    • 此解决方案似乎不再适用于 hibernate.validator 7.0.1.Final。不知道怎么解决
    【解决方案2】:

    不,您不能从约束验证器中访问已验证元素的名称。但是,它会因违反约束而暴露出来(通过getPropertyPath())。

    【讨论】:

      【解决方案3】:

      在hibernate实现中,可以通过反射ConstraintValidatorContext.basePath.currentLeafNode.name来获取

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 2012-02-22
        • 1970-01-01
        相关资源
        最近更新 更多