Spring中使用Validator进行数据验证

常用的Validator

@NotBlank 非空校验,表示不能为空,包括null“”的校验,至少包含一个非空字符,适用于CharSequence

@NotNull 非空校验,表示不能为null,适用于任何类型

@NotEmpty 非空校验,表示不能为空字符串或CharSequenceCollectionMapArray不能为空集合

@Min/@Max 最大值、最小值校验

@Size 长度校验,适用于CharSequenceCollectionMapArray

@Email 邮件格式校验

@Pattern 正则校验

基本用法
在实体定义中,属性或构建函数增加相应的注解,如下:
package com.garlic.domain;

import com.garlic.validation.constraints.TransNoValidator;

import javax.validation.constraints.*;
import java.io.Serializable;

/**
 * 用户模型
 *
 * @author sam.liu
 * @create 2017-10-19 09:16
 * @contact 563750241
 * @email [email protected]
 */
public class User implements Serializable {

    private static final long serialVersionUID = -1925045206419899653L;
    @NotNull(message = "Id不能为空")
    @Max(value = 1000)
    private Long id;

    @NotEmpty
    @Size(min = 1, max = 10, message = "")
    private String name;

    /**
     * 以ZX开头 用-隔开 以数字结尾
     */
    @TransNoValidator
    private String transNo;

    @NotBlank
    @Email
    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTransNo() {
        return transNo;
    }

    public void setTransNo(String transNo) {
        this.transNo = transNo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
在Controller中,捕捉不符合条件的校验结果
package com.garlic.controller;

import com.garlic.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用于学习Bean Validator,包括使用系统自带的Bean Validator 与 自定义Validator
 *
 * @author sam.liu
 * @create 2017-10-23 09:33
 * @contact 563750241
 * @email [email protected]
 */
@RestController
public class ValidatorController {

    private static final Logger logger = LoggerFactory.getLogger(ValidatorController.class);

    @PostMapping(value = "/validator/user/save")
    public User save(@Valid @RequestBody User user, BindingResult result) {
        result.getAllErrors().forEach(objectError -> {
            System.out.println("==========");
            FieldError fieldError = (FieldError) objectError;
            System.out.printf("Field: %s Message: %s \n",
                    fieldError.getField(),
                    fieldError.getDefaultMessage());
        });
        return user;
    }

}

以上方式,必须针对每个Controller进行分别处理,亦可使用全局异常处理的方式进行统一的拦截,然后转换成适合项目的数据格式,比如JSON

package com.garlic.handler;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 异常切面
 *
 * @author sam.liu
 * @create 2017-10-23 11:18
 * @contact 563750241
 * @email [email protected]
 */
@RestControllerAdvice
public class ApiExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Map handleException(Throwable ex) {
        MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
        BindingResult result = exception.getBindingResult();
        List fieldErrors = result.getFieldErrors();
        Map errorMap = new HashMap<>(16);
        fieldErrors.forEach(objectError -> {
                    FieldError fieldError = (FieldError) objectError;
                    System.out.printf("Field: %s Message: %s \n",
                            fieldError.getField(),
                            fieldError.getDefaultMessage());
                    errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
                }
        );
        return errorMap;
    }

}
自定义Validator
package com.garlic.validation.constraints;

import com.garlic.validation.TransNoConstraintValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * TransNoValidator
 *
 * @author sam.liu
 * @create 2017-10-23 09:53
 * @contact 563750241
 * @email [email protected]
 */
@Target({ FIELD })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { TransNoConstraintValidator.class })
public @interface TransNoValidator {

    String message() default "{com.garlic.validation.constraints.TransNoValidator.message}";

    Class[] groups() default { };

    Class[] payload() default { };

}
Validator消息格式化
  • ValidationMessages.properties

    com.garlic.validation.constraints.TransNoValidator.message=\
    Trans No must start with ZX, and the suffix is numeric.
  • ValidateMessages_zh-CN.properties

    com.garlic.validation.constraints.TransNoValidator.message=\
    交易流水号必须以ZX开头,以数字结尾

你可能感兴趣的:(Java)