个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~
个人主页:.29.的博客
学习社区:进去逛一逛~
在开发中,我们经常遇到参数校验的需求,比如用户注册的时候,要校验用户名不能为空、用户名长度不超过20个字符、手机号是合法的手机号格式等等。
如果使用普通方式,我们会把校验的代码和真正的业务处理逻辑耦合在一起,而且如果未来要新增一种校验逻辑也需要在修改多个地方。
而spring validation允许通过注解的方式来定义对象校验规则,把校验和业务逻辑分离开,让代码编写更加方便。
Spring Validation其实就是对Hibernate Validator进一步的封装,方便在Spring中使用。
Spring提供了数种数据校验的方式:
引入Maven依赖
:
<dependencies>
<dependency>
<groupId>org.hibernate.validatorgroupId>
<artifactId>hibernate-validatorartifactId>
<version>7.0.5.Finalversion>
dependency>
<dependency>
<groupId>org.glassfishgroupId>
<artifactId>jakarta.elartifactId>
<version>4.0.1version>
dependency>
dependencies>
注意:上述依赖还不够,请记得添加spring的基础依赖和Junit测试等依赖
这是一个用于测试校验的实体类
:
/**
* @author .29.
* @create 2023-03-01 10:58
*/
//创建实体类,定义属性及方法,供校验测试
public class Person {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是一个实现了Validator接口的实体类
:
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
/**
* @author .29.
* @create 2023-03-01 11:00
*/
//validator接口方式实现 校验
public class PersonValidator implements Validator {
@Override
public boolean supports(Class<?> aClass) {
return Person.class.equals(aClass); //比对实体类的类对象与参数是否一致
}
@Override
public void validate(Object o, Errors errors) { //重写校验方法
//设置name为空时,报错:name.empty
ValidationUtils.rejectIfEmpty(errors,"name","name.empty");
//传入对象,强转为实体类Person对象
Person p = (Person)o;
if(p.getAge() < 0){ //设置age属性小于零时报错
errors.rejectValue("age","error (age < 0)");
}else if(p.getAge() > 110){//设置age属性大于110报错
errors.rejectValue("age","error (age > 110) too old !!");
}
}
}
校验测试
:
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
/**
* @author .29.
* @create 2023-03-01 11:21
*/
public class testPersonValidator {
public static void main(String[] args){
Person person = new Person();
// person.setName("高启强");
// person.setAge(29);
//创建person对象的DataBinder
DataBinder binder = new DataBinder(person);
//设置校验
binder.setValidator(new PersonValidator());
//校验(当person属性值为空时,校验不通过)
binder.validate();
//输出校验结果
//binder.getBindingResult() 获取校验结果对象
//bindingResult.getAllErrors() 获取所有校验到的错误
BindingResult bindingResult = binder.getBindingResult();
System.out.println(bindingResult.getAllErrors());
}
}
没有传入参数时,对象为空,得到 name.empty 的校验错误:
[Field error in object ‘target’ on field ‘name’: rejected value [null]; codes [name.empty.target.name,name.empty.name,name.empty.java.lang.String,name.empty]; arguments []; default message [null]]
传入正确参数后,没有校验错误可输出:
[]
使用Bean Validation校验方式,就是如何将Bean Validation需要使用的javax.validation.ValidatorFactory 和javax.validation.Validator注入到容器中。spring默认有一个实现类LocalValidatorFactoryBean
,它实现了上面Bean Validation中的接口,并且也实现了org.springframework.validation.Validator接口。
这是一个配置了配置LocalValidatorFactoryBean的配置类
:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
/**
* @author .29.
* @create 2023-03-01 11:34
*/
//配置类
@Configuration //声明为Spring配置类
@ComponentScan("com.haojin.spring.config") //设置需要扫描的包
public class ValidationConfig {
@Bean //返回值放入ioc容器
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
}
这是一个用于测试校验的实体类
:
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
/**
* @author .29.
* @create 2023-03-01 11:36
*/
//实体类,使用注解定义校验规则
public class User {
@NotNull //不可为空
private String name;
@Min(0) //最小值
@Max(110) //最大值
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;
}
}
这里总结一下常见的注解:
接下来设置校验器(两种方式):
校验器一号
:
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author .29.
* @create 2023-03-01 11:39
*/
//使用java原生的jakarta.validation.Validator校验
@Service
public class MyService1 {
@Autowired //自动装配Validator对象
private Validator validator;
//校验方法
public boolean validator(User user){
//校验后的结果存放进Set集合
Set<ConstraintViolation<User>> set = validator.validate(user);
//若没有校验到错误,集合为空,返回true。
return set.isEmpty();
}
}
校验器一号
:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.Validator;
/**
* @author .29.
* @create 2023-03-02 9:24
*/
//使用spring提供的validate校验方法
@Service
public class MyService2 {
@Autowired
private Validator validator;
public boolean validator2(User user){
BindException bindException = new BindException(user,user.getName());
validator.validate(user,bindException); //调用校验方法进行校验
System.out.println(bindException.getAllErrors()); //输出所有错误信息
return bindException.hasErrors(); //若没有异常,返回false
}
}
创建测试类,分别测试两种校验器
:
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author .29.
* @create 2023-03-02 9:33
*/
public class testBeanValidator {
@Test
public void testValidatorOne(){
//获取context对象
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ValidationConfig.class);
//校验器的实现类对象
MyService1 myValidatorOne = context.getBean(MyService1.class);
User user = new User();
boolean validator = myValidatorOne.validator(user);
System.out.println(validator);
}
@Test
public void testValidatorTwo(){
//获取context对象
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ValidationConfig.class);
//校验器的实现类对象
MyService2 myValidatorTwo = annotationConfigApplicationContext.getBean(MyService2.class);
User user = new User();
boolean validator = myValidatorTwo.validator2(user);
System.out.println(validator);
}
}
这是一个配置了MethodValidationPostProcessor的配置类
:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
/**
* @author .29.
* @create 2023-03-02 18:28
*/
//配置了MethodValidationPostProcessor的配置类
@Configuration
@ComponentScan("com.haojin.spring.three")
public class validationPostProcessor {
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor(){
return new MethodValidationPostProcessor();
}
}
这是一个测试校验的实体类,校验规则通过注解设置
import jakarta.validation.constraints.*;
/**
* @author .29.
* @create 2023-03-02 18:30
*/
public class User {
@NotNull
private String name;
@Min(0)
@Max(129)
private int age;
//手机号格式 1开头 第二位是(3、4、6、7、9)其一,后面是9位数字
@Pattern(regexp = "^1(3|4|6|7|9)\\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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
}
定义Service类,通过注解操作对象
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
/**
* @author .29.
* @create 2023-03-02 18:35
*/
@Service
@Validated
public class MyService { //定义Service类,通过注解操作对象
public String testParams(@NotNull @Valid User user){
return user.toString();
}
}
测试
:
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author .29.
* @create 2023-03-02 18:37
*/
public class TestMethod {
@Test
public void testMyService(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(validationPostProcessor.class);
MyService bean = context.getBean(MyService.class);
User user = new User();
bean.testParams(user);
}
}
自定义设置校验规则的注解
:
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();
}
}
编写校验类
:
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
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;
}
}