当前位置:网站首页>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();
}
}
边栏推荐
- 人穷志不短,穷学生也能玩转树莓派
- ArrayList analysis 1-cycle, capacity expansion, version
- 技术人进阶画业务大图,手把手教学来了
- pytorch编程知识(2)
- Oracle table creation and management
- Win11安装redis 数据库以及redis desktop manager的下载
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- 5. TPM module initialization
- Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
- Exercise and health
猜你喜欢

Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support

冲击继电器ZC-23/DC220V

High quality pump SolidWorks model material recommended, not to be missed

Exercises on recursion in C language

Dx-11q signal relay

Principes de formation de la programmation robotique

Locking relay ydb-100, 100V

Metauniverse and virtual reality (II)

Kongyiji's first question: how much do you know about service communication?

Exploring the road of steam education innovation in the Internet Era
随机推荐
DLS-20型双位置继电器 220VDC
Oracle table creation and management
Packing and unpacking of C #
Xjy-220/43ac220v static signal relay
【原创】 PLSQL 索引排序优化
【go】go 实现行专列 将集合进行转列
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
染色法判断二分图
Exploring the road of steam education innovation in the Internet Era
New content violation degree determination scana bad information monitoring capability update issue 5
Experiment 8 T-SQL, stored procedure
Impact relay zc-23/dc220v
Length of the longest integrable subarray
探索互联网时代STEAM教育创新之路
Pytorch installs and uses GPU acceleration
K210工地安全帽
The principle of journal node
Exercise and health
What if the disk of datanode is full?
Using asyncio for concurrency