当前位置:网站首页>自定义注解实现校验
自定义注解实现校验
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();
}
}
边栏推荐
- 蒹葭苍苍,白露为霜。
- pytorch编程知识(2)
- PyTorch安装并使用gpu加速
- 双位置继电器DLS-5/2 DC220V
- Cmu15445 (fall 2019) project 1 - buffer pool details
- Member management applet actual development 07 page Jump
- Set different background colors for the border and text of the button
- Share your own terminal DIY display banner
- The communication mechanism and extension of Supervisor
- Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
猜你喜欢
随机推荐
The longest selling mobile phone in China has been selling well since its launch, crushing iphone12
Oracle data integrity
HDU 2488 A Knight's Journey(DFS)
Share your own terminal DIY display banner
Mindjet mindmanager2022 mind map decompression installer tutorial
What if the disk of datanode is full?
Koa koa-combine-routers 分路由管理
DLS-20型双位置继电器 220VDC
Set different background colors for the border and text of the button
C language file operation for conquering C language
None of the following candidates is applicable because of a receiver type mismatch
Hoo research | coinwave production - nym: building the next generation privacy infrastructure
Multi graph explanation of resource preemption in yarn capacity scheduling
Cmu15445 (fall 2019) project 1 - buffer pool details
ESP8266 RC522
TCP三次握手为什么不是两次或四次
The communication mechanism and extension of Supervisor
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
Pytorch auto derivation
Some views on libco









