当前位置:网站首页>Custom annotation
Custom annotation
2022-07-03 15:21:00 【You are my forever bug】
With Notes for checking mobile number IsMobile For example
Code cloud
1、 Write one first Method of mobile number inspection rules
/** * Inspection data format */
public class CheckUtil {
private static final Pattern mobile_patten = Pattern.compile("[1]([3-9])[0-9]{9}$");
/** * Mobile number verification * * @param mobile * @return boolean * @author LC * @operation add * @date 2:19 Afternoon 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、 The class specifically implemented in the annotation
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContex
public class CheckMobile implements ConstraintValidator<IsMobile,String> {
private boolean require = false;
// initialization
@Override
public void initialize(IsMobile constraintAnnotation) {
require = constraintAnnotation.required();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
// If required , Direct verification
if (require){
return CheckUtil.isMobile(value);
}else {
// If not required , Then judge whether it is empty , If it is not empty, verify
if (StringUtils.isEmpty(value)){
return true;
}else {
return CheckUtil.isMobile(value);
}
}
}
}
3、 Custom annotation
/** * Verify the mobile number annotation Custom annotation */
@Documented
@Constraint(validatedBy = {
CheckMobile.class })
@Target({
METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface IsMobile {
// Required by default
boolean required() default true;
String message() default " Phone number Format error ";
Class<?>[] groups() default {
};
Class<? extends Payload>[] payload() default {
};
}
Reading :
3 Medium @Constraint(validatedBy = {CheckMobile.class }) The class specifically implemented for this annotation
2 Medium implements ConstraintValidator<IsMobile,String> Medium IsMobile Name for custom annotation ,String This annotation is used for the data type of the parameter
边栏推荐
- Finally, someone explained the financial risk management clearly
- mysql innodb 存储引擎的特性—行锁剖析
- [transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
- 需要知道的字符串函数
- High quality workplace human beings must use software to recommend, and you certainly don't know the last one
- Halcon与Winform学习第二节
- 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
- Jvm-09 byte code introduction
- What is label encoding? How to distinguish and use one hot encoding and label encoding?
- 【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
猜你喜欢

Digital image processing -- popular understanding of corrosion and expansion

qt使用QZxing生成二维码
![[pytorch learning notes] datasets and dataloaders](/img/c0/9cd539caff34db3cccc44505bbe3c5.png)
[pytorch learning notes] datasets and dataloaders

Popular understanding of linear regression (II)

Jvm-04-runtime data area heap, method area

秒杀系统2-Redis解决分布式Session问题
![[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer](/img/9b/6ca8375ef8689a80d437665909ae30.png)
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer

Chapter 04_ Logical architecture

Jvm-05-object, direct memory, string constant pool

Dataframe returns the whole row according to the value
随机推荐
Apache ant extension tutorial
开启 Chrome 和 Edge 浏览器多线程下载
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
el-switch 赋值后状态不变化
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
XWiki安装使用技巧
What is label encoding? How to distinguish and use one hot encoding and label encoding?
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
Enable multi-threaded download of chrome and edge browsers
需要知道的字符串函数
Analysis of development mode process based on SVN branch
socket.io搭建分布式Web推送服务器
socket. IO build distributed web push server
[probably the most complete in Chinese] pushgateway entry notes
Tensorflow realizes verification code recognition (I)
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
Visual upper system design and development (Halcon WinForm) -1 Process node design
C语言刷题~Leetcode与牛客网简单题
Summary of concurrent full knowledge points