英文:
How to put YML value inside the @Pattern(regexp = "HELLO|WORLD")
问题
我想将“HELLO|WORLD”这个值移动到YML文件中。然后在正则表达式内部调用YML文件中的值。
例如,
以下是YAML文件
YML文件
valid-caller-value: HELLO|WORLD
用于获取YAML值的Java类
@Configuration
@ConfigurationProperties
@Data
@NoArgsConstructor
public class Properties {
private String validCallerValue;
}
使用正则表达式验证的Java类
@Data
@AllArgsConstructor
@NoArgsConstructor
@SearchCriteriaValidator
public class classOne {
@Pattern(regexp = "HELLO|WORLD", flags = Pattern.Flag.CASE_INSENSITIVE, message = "callerValue key has invalid value. No leading or trailing space(s) are allowed")
protected String callerValue;
}
与使用字符串不同,我想要做类似于这样的事情。
@Pattern(regexp = properties.getValidCallerValue())
我已经尝试过以下注释,但都没有成功。
@Pattern(regexp = "#\\{@properties.getValidCallerValue()}")
@Pattern(regexp = "${properties.getValidCallerValue()}
是否有可能实现这一点?
注意:我真的不想使用一个常量。
英文:
I want to move the "HELLO|WORLD" value to the YML file. Then call the value from YML file inside the regexp.
For example,
Following is the YAML file
YML FILE
valid-caller-value: HELLO|WORLD
Java class to get the YAML value
@Configuration
@ConfigurationProperties
@Data
@NoArgsConstructor
public class Properties {
private String validCallerValue;
}
Java class that use regex validation
@Data
@AllArgsConstructor
@NoArgsConstructor
@SearchCriteriaValidator
public class classOne {
@Pattern(regexp = ""HELLO|WORLD", flags = Pattern.Flag.CASE_INSENSITIVE, message = "callerValue key has invalid value. No leading or trailing space(s) are allowed")
protected String callerValue;
}
Instead of a string, I want to do something similar to this.
@Pattern(regexp = properties.getValidCallerValue())
I have already tried the following annotation. And none of them worked
@Pattern(regexp = "#\\{@properties.getValidCallerValue()}")
@Pattern(regexp = "$\\{properties.getValidCallerValue()}
Is it possible to achieve this?
NOTE: I don't really want to use a constant.
答案1
得分: 0
我不知道在@Pattern
注解中使用SpEL的方法。有一种将正则表达式外部化的方法,但这涉及创建自己的验证注解。我不知道这是否适用于您,但像这样的东西应该可以工作。
注解
@Target({ FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = PropertyPatternValidator.class)
@Documented
public @interface PropertyPattern {
String message() default "value doesn't match pattern";
String property();
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
验证器
public class PropertyPatternValidator implements ConstraintValidator<PropertyPattern, String> {
private Pattern pattern;
@Override
public void initialize(PropertyPattern propertyPattern) {
final String property = propertyPattern.property();
final Environment environment = SpringContextHolder.getBean(Environment.class);
pattern = Pattern.compile(environment.getProperty(property));
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
}
英文:
I don't know of any way to use SpEL in a @Pattern
annotation. There is a way to externalize regexp's like that, but it involves creating your own validation annotation. I don't know if that is an option for you, but something like this should work.
Annotation
@Target({ FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = PropertyPatternValidator.class)
@Documented
public @interface PropertyPattern {
String message() default "value doesn't match pattern";
String property();
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Validator
public class PropertyPatternValidator implements ConstraintValidator<PropertyPattern, String> {
private Pattern pattern;
@Override
public void initialize(PropertyPattern propertyPattern) {
final String property = propertyPattern.property();
final Environment environment = SpringContextHolder.getBean(Environment.class);
pattern = Pattern.compile(environment.getProperty(property));
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论