当前位置:网站首页>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
边栏推荐
- Kubernetes带你从头到尾捋一遍
- Introduction to redis master-slave, sentinel and cluster mode
- 如何使用 @NotNull等注解校验 并全局异常处理
- Detailed comments on MapReduce instance code on the official website
- Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
- What is label encoding? How to distinguish and use one hot encoding and label encoding?
- 整形和浮点型是如何在内存中的存储
- Win10 enterprise 2016 long term service activation tutorial
- socket. IO build distributed web push server
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
猜你喜欢

求字符串函数和长度不受限制的字符串函数的详解

Halcon and WinForm study section 1

【可能是全中文网最全】pushgateway入门笔记

Digital image processing -- popular Canny edge detection

Kubernetes vous emmène du début à la fin

Summary of concurrent full knowledge points

Idea does not specify an output path for the module
![[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

Halcon and WinForm study section 2

Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
随机推荐
redis缓存穿透,缓存击穿,缓存雪崩解决方案
开启 Chrome 和 Edge 浏览器多线程下载
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
The first character of leetcode sword offer that only appears once (12)
Calibre LVL
北京共有产权房出租新规实施的租赁案例
Using notepad++ to build an arbitrary language development environment
Digital image processing -- popular Canny edge detection
Jvm-04-runtime data area heap, method area
整形和浮点型是如何在内存中的存储
解决pushgateway数据多次推送会覆盖的问题
Global and Chinese markets for ionization equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Popular understanding of gradient descent
什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
Enable multi-threaded download of chrome and edge browsers
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Basic SQL tutorial
Influxdb2 sources add data sources
socket.io搭建分布式Web推送服务器
Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing