当前位置:网站首页>User defined annotation implementation verification
User defined annotation implementation verification
2022-07-01 01:18:00 【Violence produces miracles】
Take checking the mobile phone number as an example
First you need to prepare a validation Component's maven rely on
<!--validation Components -->
<!-- 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>
Definition notes
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 "{ Incorrect format of mobile phone number }";
Class<?>[] groups() default {
};
Class<? extends Payload>[] payload() default {
};
}
among IsMobileValidator.class You also need to define it yourself , Indicates the specific verification method
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}$");
/** * Mobile number verification * @param mobile * @return */
public static boolean isMobile(String mobile){
if(StringUtils.isEmpty(mobile)){
return false;
}
Matcher matcher=mobile_pattern.matcher(mobile);
return matcher.matches();
}
}
边栏推荐
- 运动与健康
- 【go】go 实现行专列 将集合进行转列
- Inspire students' diversified thinking with steam Education
- ESP8266 RC522
- Locking relay ydb-100, 100V
- Sword finger offer 19 Regular Expression Matching
- HDU 2488 A Knight's Journey(DFS)
- K210 site helmet
- [2023 MediaTek approved the test questions in advance] ~ questions and reference answers
- 酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
猜你喜欢
随机推荐
C # generates PPK files in putty format (supports passphrase)
ArrayList analysis 1-cycle, capacity expansion, version
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
The principle of journal node
Windows环境下安装MongoDB数据库
What is product thinking
Web interface testing of software testing
Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
Problem solving: how to manage thread_local pointer variables
Locking relay ydb-100, 100V
What is the difference between Pipeline and Release Pipeline in azure devops?
分割链表[先取next再斩断链表防止断链]
Using asyncio for concurrency
Service
Sword finger offer 18 Delete the node of the linked list
How to scroll uitableview to a specific position - how to scroll uitableview to specific position
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
The question of IBL precomputation is finally solved
【原创】 PLSQL 索引排序优化
初识 Flutter 的绘图组件 — CustomPaint









