Spring6 数据校验 Validation
1、Spring Validation概述
在开发中,经常遇到参数校验的需求,比如用户注册的时候,要校验用户名不能为空、用户名长度不超过20个字符、手机号是合法的手机号格式等等。如果使用普通方式,会把校验的代码和真正的业务处理逻辑耦合在一起,而且如果未来要新增一种校验逻辑也需要在修改多个地方。而spring validation允许通过注解的方式来定义对象校验规则,把校验和业务逻辑分离开,让代码编写更加方便。Spring Validation其实就是对Hibernate Validator进一步的封装,方便在Spring中使用。
在Spring中有多种校验的方式
第一种是通过实现org.springframework.validation.Validator接口,然后在代码中调用这个类
第二种是按照Bean Validation方式来进行校验,即通过注解的方式。
第三种是基于方法实现校验
除此之外,还可以实现自定义校验
2、通过Validator接口实现
第一步 创建模块 spring6-validator
第二步 引入相关依赖
org.hibernate.validator hibernate-validator 7.0.5.Final org.glassfish jakarta.el 4.0.1
第三步 创建实体类,定义属性和方法
package com.example.spring6.validation.method1;public class Person {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
第四步 创建类实现Validator接口,实现接口方法指定校验规则
package com.example.spring6.validation.method1;import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;public class PersonValidator implements Validator {@Overridepublic boolean supports(Class> clazz) {return Person.class.equals(clazz);}@Overridepublic void validate(Object object, Errors errors) {ValidationUtils.rejectIfEmpty(errors, "name", "name.empty");Person p = (Person) object;if (p.getAge() < 0) {errors.rejectValue("age", "error value < 0");} else if (p.getAge() > 110) {errors.rejectValue("age", "error value too old");}}
}
上面定义的类,其实就是实现接口中对应的方法,
supports
方法用来表示此校验用在哪个类型上,
validate
是设置校验逻辑的地点,其中ValidationUtils
,是Spring封装的校验工具类,帮助快速实现校验。
第五步 使用上述Validator进行测试
package com.example.spring6.validation.method1;import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;public class TestMethod1 {public static void main(String[] args) {//创建person对象Person person = new Person();person.setName("lucy");person.setAge(-1);// 创建Person对应的DataBinderDataBinder binder = new DataBinder(person);// 设置校验binder.setValidator(new PersonValidator());// 由于Person对象中的属性为空,所以校验不通过binder.validate();//输出结果BindingResult results = binder.getBindingResult();System.out.println(results.getAllErrors());}
}
3、Bean Validation注解实现
使用Bean Validation
校验方式,就是如何将Bean Validation需要使用的javax.validation.ValidatorFactory
和javax.validation.Validator
注入到容器中。spring默认有一个实现类LocalValidatorFactoryBean
,它实现了上面Bean Validation中的接口,并且也实现了org.springframework.validation.Validator接口。
第一步 创建配置类,配置LocalValidatorFactoryBean
@Configuration
@ComponentScan("com.example.spring6.validation.method2")
public class ValidationConfig {@Beanpublic LocalValidatorFactoryBean validator() {return new LocalValidatorFactoryBean();}
}
第二步 创建实体类,使用注解定义校验规则
package com.example.spring6.validation.method2;import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;public class User {@NotNullprivate String name;@Min(0)@Max(120)private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
常用注解说明
第三步 使用两种不同的校验器实现
(1)使用jakarta.validation.Validator校验
package com.example.spring6.validation.method2;import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;@Service
public class MyService1 {@Autowiredprivate Validator validator;public boolean validator(User user){Set> sets = validator.validate(user);return sets.isEmpty();}}
(2)使用org.springframework.validation.Validator校验
package com.example.spring6.validation.method2;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.Validator;@Service
public class MyService2 {@Autowiredprivate Validator validator;public boolean validaPersonByValidator(User user) {BindException bindException = new BindException(user, user.getName());validator.validate(user, bindException);return bindException.hasErrors();}
}
第四步 测试
package com.example.spring6.validation.method2;import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMethod2 {@Testpublic void testMyService1() {ApplicationContext context = new AnnotationConfigApplicationContext(ValidationConfig.class);MyService1 myService = context.getBean(MyService1.class);User user = new User();user.setAge(-1);boolean validator = myService.validator(user);System.out.println(validator);}@Testpublic void testMyService2() {ApplicationContext context = new AnnotationConfigApplicationContext(ValidationConfig.class);MyService2 myService = context.getBean(MyService2.class);User user = new User();user.setName("lucy");user.setAge(130);user.setAge(-1);boolean validator = myService.validaPersonByValidator(user);System.out.println(validator);}
}
4、基于方法实现校验
第一步 创建配置类,配置MethodValidationPostProcessor
package com.example.spring6.validation.method3;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;@Configuration
@ComponentScan("com.example.spring6.validation.method3")
public class ValidationConfig {@Beanpublic MethodValidationPostProcessor validationPostProcessor() {return new MethodValidationPostProcessor();}
}
第二步 创建实体类,使用注解设置校验规则
package com.example.spring6.validation.method3;import jakarta.validation.constraints.*;public class User {@NotNullprivate String name;@Min(0)@Max(120)private int age;@Pattern(regexp = "^1(3|4|5|7|8)\d{9}$",message = "手机号码格式错误")@NotBlank(message = "手机号码不能为空")private String phone;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}
}
第三步 定义Service类,通过注解操作对象
package com.example.spring6.validation.method3;import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;@Service
@Validated
public class MyService {public String testParams(@NotNull @Valid User user) {return user.toString();}}
第四步 测试
package com.example.spring6.validation.method3;import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMethod3 {@Testpublic void testMyService1() {ApplicationContext context = new AnnotationConfigApplicationContext(ValidationConfig.class);MyService myService = context.getBean(MyService.class);User user = new User();user.setAge(-1);myService.testParams(user);}
}
5、实现自定义校验
第一步 自定义校验注解
package com.example.spring6.validation.method4;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.example.spring6.validation.method4;import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;public class CannotBlankValidator implements ConstraintValidator {@Overridepublic void initialize(CannotBlank constraintAnnotation) {}@Overridepublic 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;}
}
标签:
相关文章
-
无相关信息