第一步 自定义校验注解
package com.atguigu.Valildator4; import com.atguigu.Valildator4.CannotBlankValidator.CannotBlankValidator; import jakarta.validation.Constraint; import jakarta.validation.Payload; import java.lang.annotation.*; @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented @Constraint(validatedBy = {CannotBlankValidator.class}) public @interface CannotBlank { //默认错误信息 String message() default "不能包含空格"; //分组 Class<?>[] groups() default {}; //负载 Class<? extends Payload>[] payload() default {}; //指定多个时使用 @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface List { CannotBlank[] value(); } }
第二步 编写真正的校验类
package com.atguigu.Valildator4.CannotBlankValidator; import com.atguigu.Valildator4.CannotBlank; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; import java.lang.annotation.Annotation; public class CannotBlankValidator implements ConstraintValidator<CannotBlank,String > { @Override public void initialize(CannotBlank constraintAnnotation) { } @Override public boolean isValid(String value, ConstraintValidatorContext context) { //null时不进行校验 if (value != null && value.contains(" ")) { //获取默认提示信息 String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate(); System.out.println("default message :" + defaultConstraintMessageTemplate); //禁用默认提示信息 context.disableDefaultConstraintViolation(); //设置提示语 context.buildConstraintViolationWithTemplate("can not contains blank").addConstraintViolation(); return false; } return true; } }
测试
、
package com.atguigu.Validator3; import com.atguigu.Valildator4.CannotBlank; import jakarta.validation.constraints.*; public class User { @NotNull private String name; @Max(120) @Min(0) private int age; @Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误") @NotBlank(message = "手机号不能为空") private String phone; @CannotBlank private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setPhone(String phone) { this.phone = phone; } }
package com.atguigu.Validator3; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestUser { public static void main(String[] args) { ApplicationContext context= new AnnotationConfigApplicationContext(ValidationConfig.class); Myservice myservice = context.getBean(Myservice.class); User user=new User(); user.setAge(18); user.setName("yoyo"); user.setPhone("15299698836"); user.setMsg("lll"); myservice.TestMethod(user); System.out.println(" =========== " ); } }
package com.atguigu.Valildator4;
import com.atguigu.Valildator4.CannotBlankValidator.CannotBlankValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {CannotBlankValidator.class})
public @interface CannotBlank {
//默认错误信息
String message() default "不能包含空格";
//分组
Class<?>[] groups() default {};
//负载
Class<? extends Payload>[] payload() default {};
//指定多个时使用
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
CannotBlank[] value();
}
}