当前位置:网站首页>自定义注解实现校验
自定义注解实现校验
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();
}
}
边栏推荐
猜你喜欢

剑指 Offer 18. 删除链表的节点

魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计

Vnctf 2022 cm CM1 re reproduction

P4学习——Basic Tunneling

Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak

Koa koa-combine-routers 分路由管理

ESP8266 RC522

剑指 Offer 19. 正则表达式匹配

PHP online confusion encryption tutorial sharing + basically no solution

The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
随机推荐
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
C#生成putty格式的ppk文件(支持passphrase)
Share your own terminal DIY display banner
Vnctf 2022 cm CM1 re reproduction
Metauniverse and virtual reality (II)
Fluent JSON serialization deserialization
双链表:初始化 插入 删除 遍历
Self examination before school starts
双位置继电器DLS-5/2 DC220V
2021电赛F题openmv和K210调用openmv api巡线,完全开源。
Set different background colors for the border and text of the button
获取屏幕高度
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
NE555波形发生器手把手教程之NE555内部结构(一)
06.论Redis持久化的几种方式
Web interface testing of software testing
DLS-20型双位置继电器 220VDC
合适的工作就是好工作
Basic data structure of redis
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!