当前位置:网站首页>自定义注解
自定义注解
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系列(一)——網絡可視化工具netron
- Summary of concurrent full knowledge points
- Win10 enterprise 2016 long term service activation tutorial
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
- Global and Chinese markets for infrared solutions (for industrial, civil, national defense and security applications) 2022-2028: Research Report on technology, participants, trends, market size and sh
- 【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
- SQL server安装位置改不了
- 【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- The method of parameter estimation of user-defined function in MATLAB
猜你喜欢
Besides lying flat, what else can a 27 year old do in life?
基础SQL教程
百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
Kubernetes will show you from beginning to end
Redis cache penetration, cache breakdown, cache avalanche solution
Summary of concurrent full knowledge points
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
Kubernetes 进阶训练营 Pod基础
随机推荐
Finally, someone explained the financial risk management clearly
PyTorch crop images differentiablly
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
Kubernetes - YAML文件解读
TPS61170QDRVRQ1
Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Functional modules and application scenarios covered by the productization of user portraits
Markdown file titles are all reduced by one level
在MapReduce中利用MultipleOutputs输出多个文件
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
[set theory] inclusion exclusion principle (complex example)
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
Jvm-04-runtime data area heap, method area
百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
Unity hierarchical bounding box AABB tree
Troubleshooting method of CPU surge
socket. IO build distributed web push server
leetcode_ Power of Four
Kubernetes带你从头到尾捋一遍
What is label encoding? How to distinguish and use one hot encoding and label encoding?