当前位置:网站首页>自定义注解实现校验
自定义注解实现校验
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();
}
}
边栏推荐
- Sword finger offer 19 Regular Expression Matching
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- Exercise and health
- Two-stage RO: part 1
- Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
- Join table query select generation
- Sword finger offer 18 Delete the node of the linked list
- Detailed analysis of operators i++ and ++i in JS, i++ and ++i
- 双位置继电器ST2-2L/AC220V
- Oracle table creation and management
猜你喜欢

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

CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解

Chapter 53 overall understanding of procedures from the perspective of business logic implementation

冲击继电器ZC-23/DC220V

Oracle-表的创建与管理

【go】go 实现行专列 将集合进行转列

初识 Flutter 的绘图组件 — CustomPaint

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

集群与LVS介绍及原理解析

写给 5000 粉丝的一封信!
随机推荐
Problem solving: how to manage thread_local pointer variables
What is product thinking
pytorch编程知识(2)
[original] PLSQL index sorting optimization
什么是产品思维
How to do the performance pressure test of "Health Code"
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
问题解决:如何管理线程私有(thread_local)的指针变量
CSDN common complex formula template record
Multi graph explanation of resource preemption in yarn capacity scheduling
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Pytorch installs and uses GPU acceleration
1009 product of polynomials (25 points) [PTA class A]
Usage of C set
Practical shell knowledge
TCP三次握手为什么不是两次或四次
Sword finger offer 18 Delete the node of the linked list
Line number of Jenkins pipeline script execution exception
PHP online confusion encryption tutorial sharing + basically no solution