当前位置:网站首页>自定义注解
自定义注解
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 为该注解用于参数的数据类型
边栏推荐
- Use of Tex editor
- Kubernetes will show you from beginning to end
- Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
- Kubernetes vous emmène du début à la fin
- 运维体系的构建
- The method of parameter estimation of user-defined function in MATLAB
- redis缓存穿透,缓存击穿,缓存雪崩解决方案
- Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis主从、哨兵、集群模式介绍
- 阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
猜你喜欢

Kubernetes advanced training camp pod Foundation
![[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller](/img/a4/2156b61fbf50db65fdf59c8f5538f8.png)
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller

What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding

Basic SQL tutorial

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

基础SQL教程

qt使用QZxing生成二维码

GCC cannot find the library file after specifying the link library path

Halcon与Winform学习第一节

Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
随机推荐
Halcon与Winform学习第一节
Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
整形和浮点型是如何在内存中的存储
Using notepad++ to build an arbitrary language development environment
Tensor ellipsis (three points) slice
The state does not change after the assignment of El switch
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
Incluxdb2 buckets create database
Reentrantlock usage and source code analysis
使用JMeter对WebService进行压力测试
Finally, someone explained the financial risk management clearly
Kubernetes vous emmène du début à la fin
Use of Tex editor
Win10 enterprise 2016 long term service activation tutorial
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
视觉上位系统设计开发(halcon-winform)-3.图像控件
Dataframe returns the whole row according to the value
Global and Chinese market of lighting control components 2022-2028: Research Report on technology, participants, trends, market size and share