当前位置:网站首页>自定义注解
自定义注解
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 为该注解用于参数的数据类型
边栏推荐
- SQL server安装位置改不了
- Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
- [pytorch learning notes] datasets and dataloaders
- Matlab r2011b neural network toolbox precautions
- 基于SVN分支开发模式流程浅析
- Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- Puppet自动化运维排错案例
- 【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
- Influxdb2 sources add data sources
- Leetcode sword offer find the number I (nine) in the sorted array
猜你喜欢

Redis主从、哨兵、集群模式介绍

函数栈帧的创建和销毁

视觉上位系统设计开发(halcon-winform)-4.通信管理

什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用

Basic SQL tutorial
![[transform] [NLP] first proposed transformer. The 2017 paper](/img/33/f639ab527d5adedfdc39f8d8117c3e.png)
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team

Idea does not specify an output path for the module

Kubernetes advanced training camp pod Foundation
![[probably the most complete in Chinese] pushgateway entry notes](/img/5a/6dcb75f5d713ff513ad6842ff53cc3.png)
[probably the most complete in Chinese] pushgateway entry notes

Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
随机推荐
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
基础SQL教程
Summary of concurrent full knowledge points
Win10 enterprise 2016 long term service activation tutorial
There are links in the linked list. Can you walk three steps faster or slower
qt使用QZxing生成二维码
Apache ant extension tutorial
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
北京共有产权房出租新规实施的租赁案例
Dataframe returns the whole row according to the value
第04章_逻辑架构
What is machine reading comprehension? What are the applications? Finally someone made it clear
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
Tensor ellipsis (three points) slice
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
The state does not change after the assignment of El switch
使用JMeter对WebService进行压力测试
socket. IO build distributed web push server