2
我尝试@Pattern
标注为验证输入如何获得java中的注释值?
@Pattern(regexp="[A-Za-z0-9_.]*", message="Name can only contain alphanumeric characters and underscores and dots")
public String getName() {
return name;
}
我也想错误message
包含已传递的name
的实际值。我如何在批注调用中访问它? e.g,我想是这样的:
@Pattern(regexp="[A-Za-z0-9_.]*", message="Name can only contain alphanumeric characters and underscores and dots. Provided name: " + name + " is not valid")
我尝试这样做,收到错误消息The value for annotation attribute Pattern.message must be a constant expression
。有什么方法可以在注释定义中获得实际的变量值(特别是错误消息)吗?我想通过显示实际提供的值来使错误消息更有帮助。