当前位置:网站首页>自定义注解
自定义注解
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 为该注解用于参数的数据类型
边栏推荐
- Chapter 04_ Logical architecture
- The first character of leetcode sword offer that only appears once (12)
- [set theory] inclusion exclusion principle (complex example)
- el-switch 赋值后状态不变化
- The state does not change after the assignment of El switch
- Yolov5 advanced nine target tracking example 1
- [Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
- Functional modules and application scenarios covered by the productization of user portraits
- Didi off the shelf! Data security is national security
- Remote server background hangs nohup
猜你喜欢
Série yolov5 (i) - - netron, un outil de visualisation de réseau
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
Influxdb2 sources add data sources
Construction of operation and maintenance system
Didi off the shelf! Data security is national security
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
Introduction, use and principle of synchronized
Introduction to redis master-slave, sentinel and cluster mode
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
随机推荐
Tensorflow realizes verification code recognition (I)
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
XWiki安装使用技巧
Incluxdb2 buckets create database
Kubernetes帶你從頭到尾捋一遍
百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
[probably the most complete in Chinese] pushgateway entry notes
qt使用QZxing生成二维码
Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode the smallest number of the rotation array of the offer of the sword (11)
【可能是全中文网最全】pushgateway入门笔记
Summary of JVM knowledge points
[pytorch learning notes] datasets and dataloaders
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
Qt常用语句备忘
Win10 enterprise 2016 long term service activation tutorial
【日常训练】395. 至少有 K 个重复字符的最长子串
Introduction, use and principle of synchronized
socket.io搭建分布式Web推送服务器