天天看點

SpringBoot - Bean validation 參數校驗

目錄

  • 前言
  • 常見注解
  • 參數校驗的應用
    • 依賴
    • 簡單的參數校驗示例
    • 級聯校驗
    • @Validated 與 @Valid
    • 自定義校驗注解

背景開發中對參數的校驗是不可缺少的一個環節,為了解決如何優雅的對參數進行校驗?

  • JSR303(Java Specification Requests)應運而生,JSR303 是JavaBean參數校驗的标準。
  • Bean Validation 為 JavaBean 驗證定義了相應的中繼資料模型和 API。
  • Hibernate validator 5 是 Bean Validation 1.1的實作。
  • Bean Validation中定義的注解:
注解 詳細資訊
@Null 被注釋的元素必須為 null
@NotNull 被注釋的元素必須不為 null
@AssertTrue 被注釋的元素必須為 true
@AssertFalse 被注釋的元素必須為 false
@Min(value) 被注釋的元素必須是一個數字,其值必須大于等于指定的最小值
@Max(value) 被注釋的元素必須是一個數字,其值必須小于等于指定的最大值
@DecimalMin(value)
@DecimalMax(value)
@Size(max, min) 被注釋的元素的大小必須在指定的範圍内
@Digits (integer, fraction) 被注釋的元素必須是一個數字,其值必須在可接受的範圍内
@Past 被注釋的元素必須是一個過去的日期
@Future 被注釋的元素必須是一個将來的日期
@Pattern(value) 被注釋的元素必須符合指定的正規表達式
  • Hibernate validator 在JSR303的基礎上對校驗注解進行了擴充,擴充注解如下:
@Email 被注釋的元素必須是電子郵箱位址
@Length 被注釋的字元串的大小必須在指定的範圍内
@NotEmpty 被注釋的字元串的必須非空
@Range 被注釋的元素必須在合适的範圍内

  • 注:SpringBoot 2.3.0之後放棄了預設對javax.validation 的支援,需引入一下依賴,2.3.0之前spring-boot-starter-web中内置。
<!-- validation -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
           

  • 要想開啟參數校驗,需要在類上标注@Validated注解
  • 控制器
@PostMapping(value = "/test")
 public ValidOneEvt param(@RequestBody @Validated ValidOneEvt evt){
     return evt;
 }
           
  • 實體驗證
@Getter
@Builder
public class ValidOneEvt {

    @NotEmpty(message = "名稱不能為空")
    private String name;

    private String sex;
    
}
           
  • 測試結果
SpringBoot - Bean validation 參數校驗

  • 級聯校驗需要在校驗的實體上添加@Valid。
@PostMapping(value = "/test")
public ValidOneEvt param(@RequestBody @Validated ValidOneEvt evt){
    return evt;
}
           
@Getter
@Builder
public class ValidOneEvt {

    @NotEmpty(message = "名稱不能為空")
    private String name;

    private String sex;

    @Valid
    private ValidTwoEvt validTwoEvt;

}
           
  • 級聯實體
@Getter
@Setter
public class ValidTwoEvt {

    @Length(min = 2)
    private String name;

}
           
  • 請求示例
SpringBoot - Bean validation 參數校驗
  • 校驗結果
SpringBoot - Bean validation 參數校驗

兩者具有相似性

注解地方

  • @Valid:可以用在方法、構造函數、方法參數和成員屬性(字段)上。
  • @Validated:可以用在類型、方法和方法參數上,不能用在成員屬性(字段)上。

@Validated和@Valid在級聯驗證功能上的差別

  • @Valid:用在方法入參上無法單獨提供級聯驗證功能。能夠用在成員屬性(字段)上,提示驗證架構進行級聯驗證。
  • @Validated:用在方法入參上無法單獨提供級聯驗證功能。不能用在成員屬性(字段)上,也無法提示架構進行級聯驗證。能配合級聯驗證注解@Valid進行級聯驗證。
  • 總結: 通常使用@Validated, 級聯驗證使用@Valid。

  • 自定義效驗注解驗證密碼是否相等
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 * @Description 自定義參數校驗注解
 *      @Documented 注解中的注釋加入文檔
 *      @Retention 注解保留階段 RetentionPolicy.RUNTIME 運作階段
 *      @Target 作用目标,注解的使用範圍 TYPE:用于描述類、接口(包括注解類型) 或enum聲明
 *      @Constraint 将注解和注解關聯類關聯到一起
 * @author coisini
 * @date Aug 10, 2021
 * @Version 1.0
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Constraint(validatedBy = PasswordValidator.class)
public @interface PasswordEquals {

    int min() default 4;

    int max() default 6;

    String message() default "passwords are not equal";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}
           
  • 自定義校驗注解關聯類
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Description 自定義校驗注解關聯類
 *      ConstraintValidator的第一個參數:注解的類型
 *      ConstraintValidator的第二個參數:自定義注解修飾的目标的類型
 * @author coisini
 * @date Aug 10, 2021
 * @Version 1.0
 */
public class PasswordValidator implements ConstraintValidator<PasswordEquals, ValidEvt> {

    private int min;
    private int max;

    /**
     * 初始化擷取注解參數
     * @param constraintAnnotation
     */
    @Override
    public void initialize(PasswordEquals constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    /**
     * 校驗參數
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(ValidEvt value, ConstraintValidatorContext context) {
        String password1 = value.getPassword1();
        String password2 = value.getPassword2();

        return password1.equals(password2) && this.validLength(password1, password2);
    }

    /**
     * 校驗密碼長度
     * @param password1
     * @param password2
     * @return
     */
    private boolean validLength(String password1, String password2) {
        return password1.length() > min && password1.length() < max
                && password2.length() > min && password2.length() < max;
    }

}
           
  • 自定義注解校驗類
/**
 * @Description 自定義注解校驗類
 * @author coisini
 * @date Aug 10, 2021
 * @Version 1.0
 */
@Getter
@Builder
@PasswordEquals(min = 1, message = "Incorrect password length or passwords are not equal")
public class ValidEvt {

    private String password1;
    private String password2;

}
           
  • 參數驗證異常統一處理
@ControllerAdvice
public class GlobalExceptionAdvice {
	/**
     * 參數校驗異常處理器
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public UnifyMessage handleBeanValidation(HttpServletRequest request, MethodArgumentNotValidException e) {
        String method = request.getMethod();
        String requestUrl = request.getRequestURI();
        System.out.println(e);

        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        String message = formatAllErrorMessages(errors);

        return new UnifyMessage(10001, message,method + " " + requestUrl);
    }

    /**
     * 自定義注解校驗異常處理器
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public UnifyMessage handleConstrainException(HttpServletRequest req, ConstraintViolationException e){
        String method = req.getMethod();
        String requestUrl = req.getRequestURI();
        String message = e.getMessage();
        return new UnifyMessage(10001, message,method + " " + requestUrl);
    }

    /**
     * 異常消息拼接
     * @param errors
     * @return
     */
    private String formatAllErrorMessages(List<ObjectError> errors){
        StringBuffer errorMsg = new StringBuffer();
        errors.forEach(error ->
                errorMsg.append(error.getDefaultMessage()).append(";")
        );
        return errorMsg.toString();
    }
}
           
  • 統一消息傳回
/**
 * @Description 統一消息傳回
 * @author coisini
 * @date Aug 9, 2021
 * @Version 1.0
 */
public class UnifyMessage {

    private int code;
    private String message;
    private String requestUrl;

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public UnifyMessage(int code, String message, String requestUrl) {
        this.code = code;
        this.message = message;
        this.requestUrl = requestUrl;
    }

}
           
  • 測試類
@PostMapping(value = "/test1")
public ValidEvt test1(@RequestBody @Validated ValidEvt evt){
    return evt;
}
           

- End -

夢想是鹹魚

關注一下吧

SpringBoot - Bean validation 參數校驗

作者:Maggieq8324