可参数化的JSR-303验证值 [英] Parametrizable JSR-303 validation values

查看:140
本文介绍了可参数化的JSR-303验证值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSR-303 bean验证和Spring 3,我需要根据用例为注释提供不同的值。

I use JSR-303 bean validation and Spring 3 and I need to provide different values for the annotation depending on the use-case.

例如,中的值参数 @Size(min =?) 对于某些验证必须为1,对于另一个案例必须为5,我想从属性文件中读取此值。

For example, the value of min parameter in @Size(min=?) must be 1 for some validation and 5 for another case and I want to read this values from a properties file.

我知道可以从 ValidationMessages.properties 文件中读取消息参数(如果作为密钥提供)但是其他参数?

I know the message parameter can be read from ValidationMessages.properties file if provided as a key but what about the other parameter?

推荐答案

注释参数的值只能是编译时表达式。这意味着对于 @Size(min = X,max = Z) X和Z必须在编译时可解析。

The values for annotation parameters can only be compile time expressions. This means that for the @Size(min=X, max=Z) X and Z must be resolvable at compile time.

由于 min max 被声明为 int @Size 上,你被困住了。

Since min and max are declared as int on @Size, you are stuck.

如果你需要 min 的不同值,我个人认为有两种方法

If you need different values for min, I personally see two ways of doing it.

首先,您可以对验证程序使用分组。使用一组 min = 1 和一组 min = 5 。例如,让我们考虑测试类:

First, you could use a grouping on the validators. Use one group for min=1 and one group for min=5. For example, lets consider a Test class:

public class Test {
  @NotNull
  @Size.List({
     @Size(min = 1, groups = GroupOne.class),
     @Size(min = 5, groups = GroupTwo.class)
  })
  private String prop;

  public String getProp() {
     return prop;
  }
  public void setProp(String prop) {
     this.prop = prop;
  }
}

您必须申报群组:

public interface GroupOne {}
public interface GroupTwo {}

然后创建一些测试对象加上验证器来配合它:

Then create some testing object plus the validator to go with it:

Test test = new Test();
test.setProp("XY");

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

然后使用群组进行验证:

Then validate using the groups:

Set<ConstraintViolation<Test>> resultOne = validator.validate(test, GroupOne.class);
Set<ConstraintViolation<Test>> resultTwo = validator.validate(test, GroupTwo.class);

第一种情况有效,因为 min = 1 XY.length()== 2 但第二次会失败,因为 min = 5

First case is valid since min=1 and "XY".length() == 2 but second will fail because min=5.

此方法涉及手动进行验证,我认为您不能仅依靠 @Valid @RequestMapping 带注释的方法进行验证(因为 @Valid 只是验证的触发器,无法提及所需的组验证)。幸运的是,Spring非常灵活,自己打电话给验证器也不会有太大的开销。

This method involves doing the validation manually and I don't think you can just rely on @Valid on a @RequestMapping annotated method to do the validation (since @Valid is just a trigger for the validation with no way of mentioning the required group for validation). Luckly Spring is very flexible and it won't be much overhead to call the validator yourself.

我看到的第二个选项涉及创建自己的验证注释与自定义验证器匹配。 在这里,您可以找到一个简单的示例来帮助您入门。使用此解决方案,您可以将 min max 声明为验证程序将在验证之前在捆绑包中解析的字符串键。虽然这个解决方案比第一个解决方案更有开销。

The second option I see involves creating your own validation annotation with a custom validator to match. Here you can find a simple example to get you started. With this solution you can declare min and max as string keys that your validator will resolve in the bundles prior to validation. This solution though is more overhead than the first.

这篇关于可参数化的JSR-303验证值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆