@Pattern 注解可以匹配几个最终的 String 属性,而不是它们指向的字符串吗?
例如:
import javax.validation.constraints.Pattern;
public class Pojo {
public final String oneAtt = "one";
public final String twoAtt = "two";
public final String threeAtt = "three";
@Pattern(regexp = <How to match any of those attributes?> )
private String number;
由于字符串是在属性中定义的,因此目标是避免在模式中重复字符串。所以,我想避免这种情况:
@Pattern(regexp = "^(one|two|three)" )
并寻找类似的内容:
@Pattern(regexp = oneAtt|twoAtt|threeAtt )
这当然在 Java 语言中是无效的。
问题是是否有办法避免重复模式中的字符串。
For example:
import javax.validation.constraints.Pattern;
public class Pojo {
public final String oneAtt = "one";
public final String twoAtt = "two";
public final String threeAtt = "three";
@Pattern(regexp = <How to match any of those attributes?> )
private String number;
Since the strings are defined in the attributes, the goal is to avoid repeating the strings in the pattern. So, I want to avoid this:
@Pattern(regexp = "^(one|two|three)" )
And looking for something like:
@Pattern(regexp = oneAtt|twoAtt|threeAtt )
Which of course it is invalid in the Java language.
The question is if there is a way to avoid duplicating the strings in the pattern.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将这三个字符串设置为
static
,然后您可以直接在注释中使用它们:Make the three strings
static
then you can use them directly in the annotation: