当前位置:网站首页>自定义注解实现校验
自定义注解实现校验
2022-07-01 00:39:00 【卍暴力出奇迹卍】
以校验手机号码为例
首先需要准备一个validation组件的maven依赖
<!--validation组件-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.6</version>
</dependency>
定义注解
package com.zzf.seckilldemo.validator;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.NotNull;
import java.lang.annotation.*;
/** * @author zzf * @date 2022-06-26 */
@Target({
ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
validatedBy = {
IsMobileValidator.class}
)
public @interface IsMobile {
boolean required() default true;
String message() default "{手机号码格式不正确}";
Class<?>[] groups() default {
};
Class<? extends Payload>[] payload() default {
};
}
其中IsMobileValidator.class也需要自己定义,表示具体的校验方式
package com.zzf.seckilldemo.validator;
import com.zzf.seckilldemo.utils.ValidatorUtil;
import org.springframework.util.StringUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/** * @author zzf * @date 2022-06-26 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {
private boolean required = false;
@Override
public void initialize(IsMobile constraintAnnotation) {
required = constraintAnnotation.required();
}
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if(required){
return ValidatorUtil.isMobile(s);
}else {
if(StringUtils.isEmpty(s)){
return true;
}else {
return ValidatorUtil.isMobile(s);
}
}
}
}
ValidatiorUtil
package com.zzf.seckilldemo.utils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** * @author zzf * @date 2022-01-12 */
@Component
public class ValidatorUtil {
private static final Pattern mobile_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");
/** * 手机号码校验 * @param mobile * @return */
public static boolean isMobile(String mobile){
if(StringUtils.isEmpty(mobile)){
return false;
}
Matcher matcher=mobile_pattern.matcher(mobile);
return matcher.matches();
}
}
边栏推荐
- 问题解决:如何管理线程私有(thread_local)的指针变量
- PyTorch安装并使用gpu加速
- ESP8266 RC522
- Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
- 解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
- HDU 2488 A Knight's Journey(DFS)
- 初识 Flutter 的绘图组件 — CustomPaint
- Experiment 8 T-SQL, stored procedure
- HDU 2488 A Knight's Journey(DFS)
- Confirm() method of window
猜你喜欢

C#生成putty格式的ppk文件(支持passphrase)

P4学习——Basic Tunneling

Oracle临时表详解

New content violation degree determination scana bad information monitoring capability update issue 5

Sword finger offer 19 Regular Expression Matching

Pytorch auto derivation

【学习笔记】构造

解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘

Packing and unpacking of C #

Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
随机推荐
ArrayList analysis 1-cycle, capacity expansion, version
第53章 从业务逻辑实现角度整体性理解程序
Using asyncio for concurrency
History of deep learning
获取屏幕高度
什么是产品思维
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
剑指 Offer 18. 删除链表的节点
Host FL Studio fruit music production daw20.9
软硬件基础知识学习--小日记(1)
Practical shell knowledge
Error msb8031: building an MFC project for a non Unicode character set is deprecated
Get screen height
Oracle-表的创建与管理
Shift operators
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
Vnctf 2022 cm CM1 re reproduction
Oracle data integrity
蒹葭苍苍,白露为霜。
What is the difference between Pipeline and Release Pipeline in azure devops?