当前位置:网站首页>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
边栏推荐
- Functional modules and application scenarios covered by the productization of user portraits
- GCC cannot find the library file after specifying the link library path
- 【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
- Detailed comments on MapReduce instance code on the official website
- Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
- 视觉上位系统设计开发(halcon-winform)-4.通信管理
- Jvm-06-execution engine
- 在MapReduce中利用MultipleOutputs输出多个文件
- Tensorflow realizes verification code recognition (III)
- Global and Chinese markets for ionization equipment 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Introduction to redis master-slave, sentinel and cluster mode

Jvm-02-class loading subsystem

Matplotlib drawing label cannot display Chinese problems

秒杀系统3-商品列表和商品详情

mysql innodb 存储引擎的特性—行锁剖析
![[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

Characteristics of MySQL InnoDB storage engine -- Analysis of row lock

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

视觉上位系统设计开发(halcon-winform)

百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
随机推荐
Tensorflow realizes verification code recognition (II)
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
Redis cache penetration, cache breakdown, cache avalanche solution
qt使用QZxing生成二维码
The first character of leetcode sword offer that only appears once (12)
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
Jvm-03-runtime data area PC, stack, local method stack
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
整形和浮点型是如何在内存中的存储
Jvm-04-runtime data area heap, method area
秒杀系统3-商品列表和商品详情
视觉上位系统设计开发(halcon-winform)-2.全局变量设计
第04章_逻辑架构
Puppet自动化运维排错案例
开启 Chrome 和 Edge 浏览器多线程下载
Final review points of human-computer interaction
Matplotlib drawing label cannot display Chinese problems
What is label encoding? How to distinguish and use one hot encoding and label encoding?
[pytorch learning notes] transforms