当前位置:网站首页>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
- What if the disk of datanode is full?
- [2023 MediaTek approved the test questions in advance] ~ questions and reference answers
- The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
- 解读创客教育所蕴含的科技素养
- C # Generate PPK files in Putty format (passthrough support)
- Mustache syntax
- Vnctf 2022 cm CM1 re reproduction
- Error msb8031: building an MFC project for a non Unicode character set is deprecated
- 文件服务设计
猜你喜欢

Cmu15445 (fall 2019) project 1 - buffer pool details

解读创客教育所蕴含的科技素养

Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)

The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design

Sword finger offer 18 Delete the node of the linked list

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

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

The communication mechanism and extension of Supervisor

The longest selling mobile phone in China has been selling well since its launch, crushing iphone12

初识 Flutter 的绘图组件 — CustomPaint
随机推荐
【学习笔记】构造
K210门禁毕设
mustache语法
解析创客教育实践中的智慧原理
Using asyncio for concurrency
C#生成putty格式的ppk文件(支持passphrase)
Left join displays the specified value when the left join matching data is null
【go】go 实现行专列 将集合进行转列
Set different background colors for the border and text of the button
对libco的一点看法
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
leetcode 474. Ones and zeroes (medium)
Dx-11q signal relay
Length of the longest integrable subarray
Listview in flutter application development
Xjy-220/43ac220v static signal relay
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
机器人编程的培训学科类原理
Cmu15445 (fall 2019) project 1 - buffer pool details
HDU 2488 A Knight's Journey(DFS)