说明
禁止字符串包含英文逗号。
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotAllowCommaValidator.class)
public @interface NotAllowComma {
/**
* 错误提示
* @return 提示
*/
String message();
}
实现
public class NotAllowCommaValidator implements
ConstraintValidator<NotAllowComma, String> {
@Override
public void initialize(NotAllowComma constraintAnnotation) {
//nothing
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
// 包含逗号,则认为错误
if(value.contains(",")) {
return false;
}
return true;
}
}