注释不起作用:@Valid和@Pattern如何使它们工作?

问题描述

我是javax验证的新手,我无法理解它的工作原理。
我在此处搜索了很多有关stackoverflow的问题(herehere以及其他问题以及类似https://www.baeldung.com/spring-validate-list-controller的地方),但是很遗憾,没有找到解决方案。

我正在尝试制作一个简单的验证应用程序。

类似这样的东西:

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

public class Test {

  @Pattern(regexp = "abca")
  @NotNull
  public String testString;

  public Test(String string){
    testString = string;
  }

  public static void printer(@Valid Test test) {
    System.out.println(test.testString);
  }

  public static void main(String[] args) {
    Test test = new Test("qwerty");
    Test test1 = new Test(null);
    printer(test);
    printer(test1);
  }
}

我想用printer()字段注释的@PatterntestString方法进行验证,但是没有效果

有人告诉我,我应该只添加一个休眠验证依赖项(O_o),一切都会正常。
一个人-我必须写一个验证器并使用它。
其他-一切都已经可以了,这只是我的代码中的问题。

现在我决定在这里写一劳永逸的东西,使该代码正常工作需要做什么?

我知道可以有很多解决方案,例如自动bean验证,调用手写验证器的validate()方法等。

我对引起运行验证的方法@Valid注释字段感兴趣。

我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)