哪个注释用于模式匹配和字符串长度

Which annotation to use for pattern matching and string length

我正在尝试实施 bean 验证。 属性 命名的 pin 是字符串类型,并绑定到视图中用户输入的值。如您所见,引脚应仅为数字,长度应正好为 8。

if (pin.matches("[0-9]+") && pin.length() == 8 )

属性 看起来像这样,我正在尝试通过注释满足我的 objective。

    private String pin;

    // annotation syntax ?
    public String getPin() { return this.pin; }

    public void setPin(String pin) { this.pin = pin; }

我见过通过注释完成的简单 bean 验证,但我很难为上述条件创建语法。

您是否尝试过使用 @Pattern 注释? 像这样:

@Pattern(regexp = "[0-9]{8}", message = "Your error message.")
public String getPin() { return this.pin; }