当前位置:网站首页>自定义注解
自定义注解
2022-07-03 15:18:00 【ジ你是我永远のbugグ】
以 检验手机号的注解 IsMobile 为例
码云
1、先写个 手机号检验规则的方法
/** * 检验数据格式 */
public class CheckUtil {
private static final Pattern mobile_patten = Pattern.compile("[1]([3-9])[0-9]{9}$");
/** * 手机号码校验 * * @param mobile * @return boolean * @author LC * @operation add * @date 2:19 下午 2022/3/2 **/
public static boolean isMobile(String mobile) {
if (StringUtils.isEmpty(mobile)) {
return false;
}
Matcher matcher = mobile_patten.matcher(mobile);
return matcher.matches();
}
}
2、注解中具体实现的类
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContex
public class CheckMobile implements ConstraintValidator<IsMobile,String> {
private boolean require = false;
// 初始化
@Override
public void initialize(IsMobile constraintAnnotation) {
require = constraintAnnotation.required();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
// 如果必填,则直接校验
if (require){
return CheckUtil.isMobile(value);
}else {
// 如果非必填,则判断是否为空,不为空则校验
if (StringUtils.isEmpty(value)){
return true;
}else {
return CheckUtil.isMobile(value);
}
}
}
}
3、自定义注解
/** * 验证手机号注解 自定义注解 */
@Documented
@Constraint(validatedBy = {
CheckMobile.class })
@Target({
METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface IsMobile {
// 默认必填
boolean required() default true;
String message() default "手机号码 格式错误";
Class<?>[] groups() default {
};
Class<? extends Payload>[] payload() default {
};
}
解读:
3中的 @Constraint(validatedBy = {CheckMobile.class }) 为该注解具体实现的类
2中的 implements ConstraintValidator<IsMobile,String> 中的 IsMobile 为自定义注解的名称,String 为该注解用于参数的数据类型
边栏推荐
- Yolov5 advanced 8 format conversion between high and low versions
- Leetcode sword offer find the number I (nine) in the sorted array
- What is machine reading comprehension? What are the applications? Finally someone made it clear
- Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
- What is label encoding? How to distinguish and use one hot encoding and label encoding?
- Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
- Analysis of development mode process based on SVN branch
- Kubernetes - YAML文件解读
- Apache ant extension tutorial
- [transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
猜你喜欢

Kubernetes vous emmène du début à la fin
![MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)](/img/cd/2e4f5884d034ff704809f476bda288.png)
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)

Can‘t connect to MySQL server on ‘localhost‘
![Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)](/img/cd/2e4f5884d034ff704809f476bda288.png)
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)

Série yolov5 (i) - - netron, un outil de visualisation de réseau

Jvm-02-class loading subsystem
![[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention](/img/94/a9c7010fe9f14454469609ac4dd871.png)
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention

Chapter 04_ Logical architecture

What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry

函数栈帧的创建和销毁
随机推荐
el-switch 赋值后状态不变化
Yolov5 advanced seven target tracking latest environment construction (II)
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
XWiki Installation Tips
Incluxdb2 buckets create database
Analysis of development mode process based on SVN branch
Composite type (custom type)
Using Tengine to solve the session problem of load balancing
Kubernetes 进阶训练营 Pod基础
Introduction to redis master-slave, sentinel and cluster mode
Halcon与Winform学习第一节
视觉上位系统设计开发(halcon-winform)-3.图像控件
mysql innodb 存储引擎的特性—行锁剖析
SQL server安装位置改不了
What is machine reading comprehension? What are the applications? Finally someone made it clear
406. Reconstruct the queue according to height
Search in the two-dimensional array of leetcode sword offer (10)
北京共有产权房出租新规实施的租赁案例
Puppet自动化运维排错案例
[set theory] inclusion exclusion principle (complex example)