主頁 > 後端開發 > 【深度思考】如何優雅的校驗引數?

【深度思考】如何優雅的校驗引數?

2022-12-23 06:58:02 後端開發

在日常的開發作業中,為了保證落庫資料的完整性,引數校驗絕對是必不可少的一部分,本篇文章就來講解下在專案中該如何優雅的校驗引數,

假設有一個新增學員的介面,一般第一步我們都會先校驗學員資訊是否正確,然后才會落庫,簡單起見,假設新增學員時只有2個欄位:姓名、年齡,

@Data
public class StudentVO {
    /**
     * 姓名
     */
    private String name;

    /**
     * 年齡
     */
    private Integer age;
}

要求為:姓名和年齡必填,姓名不能超過20個字符,

1. 最原始的寫法

先來看下最原始的寫法,相信大多數人都這么寫過,或者說在初學Java時都這么寫過:

public String validateStudentVO(StudentVO studentVO) {
    if (StringUtils.isBlank(studentVO.getName())) {
        return "姓名不能為空";
    }
    if (studentVO.getName().length() > 20) {
        return "姓名不能超過20個字符";
    }
    if (studentVO.getAge() == null) {
        return "年齡不能為空";
    }

    return null;
}

這么寫最好理解,但一般一個專案中都會有很多介面,如果都這么寫的話,重復代碼會非常多,顯得非常臃腫,而且對于一個作業多年的開發來說,如果每天都寫這樣的代碼,會覺得特別沒有技術含量,

2. Bean Validation

既然有需求場景,就會有規范,這個規范就是Bean Validation,官網地址是 https://beanvalidation.org/,

Bean Validation先后經歷了1.0(JSR 303)、1.1(JSR 349)、2.0(JSR 380)這3個版本,目前專案中使用比較多的是Bean Validation 2.0,本篇文章講解的內容也是基于Bean Validation 2.0版本,

Bean Validation 2.0之后,現在改名叫Jakarta Bean Validation了,

pom依賴坐標如下所示:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

不過從2.0.1.Final之后的版本依賴都改為了jakarta.validation-api:

新版本pom依賴坐標如下所示:

<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>2.0.2</version>
</dependency>

3. Hibernate Validator

Hibernate Validator是 Bean Validation 的參考實作 ,不僅提供了規范中所有內置constraint的實作,除此之外還提供了一些附加的 constraint,

pom依賴坐標如下所示:

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.2.5.Final</version>
</dependency>

因為hibernate-validator中已經包含了validation-api,因此專案中如果引入了hibernate-validator,就沒必要重復引入validation-api了:

image-20221221144308018

4. Bean Validation 2.0原生注解

Bean Validation 2.0中包含了22個注解,如下圖所示:

接下來詳細講解下這22個注解的用途,

4.1 @AssertTrue

作用:被標記的元素必須為true,

支持的Java型別:boolean、Boolean,

使用示例:

@AssertTrue
private Boolean newStudent;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setNewStudent(false);

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

snipaste_20221219_162307

上面輸出的message是默認的,在實際使用時可以自定義:

@AssertTrue(message = "newStudent必須為true")
private Boolean newStudent;

效果如下圖所示:

snipaste_20221219_164946

注意事項:

1)@AssertTrue注解識別不了欄位值為null的場景:

snipaste_20221219_163148

2)如果將@AssertTrue注解使用在boolean、Boolean之外的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外:

@AssertTrue
private String name;

4.2 @AssertFalse

作用:被標記的元素值必須為false,

其余的和@AssertTrue注解一致,

使用示例:

@AssertFalse(message = "newStudent必須為false")
private Boolean newStudent;

4.3 @DecimalMax

作用:被標記的元素必須小于或等于指定的值,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、String,

使用示例:

@DecimalMax(value = "https://www.cnblogs.com/zwwhnly/archive/2022/12/22/30000")
private BigDecimal balance;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setBalance(new BigDecimal("30001"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

上面輸出的message是默認的,在實際使用時可以自定義:

@DecimalMax(value = "https://www.cnblogs.com/zwwhnly/archive/2022/12/22/30000", message = "賬戶余額必須小于或等于30000")
private BigDecimal balance;

效果如下圖所示:

snipaste_20221219_180912

注意事項:

1)@DecimalMax注解識別不了欄位值為null的場景:

2)如果將@DecimalMax注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外:

@DecimalMax(value = "https://www.cnblogs.com/zwwhnly/archive/2022/12/22/30000", message = "賬戶余額必須小于或等于30000")
private Boolean newStudent;

4.4 @DecimalMin

作用:被標記的元素值必須大于或等于指定的值,

其余的和@DecimalMax注解一致,

使用示例:

@DecimalMin(value = "https://www.cnblogs.com/zwwhnly/archive/2022/12/22/5000", message = "充值余額必須大于或等于5000")
private BigDecimal rechargeAmount;

4.5 @Digits

作用:被標記的元素整數位數和小數位數必須小于或等于指定的值,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、String,

使用示例:

@Digits(integer = 6, fraction = 2)
private BigDecimal rechargeAmount;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("100000.999"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

上面輸出的message是默認的,在實際使用時可以自定義:

@Digits(integer = 6, fraction = 2, message = "充值金額只允許6位整數、2位小數")
private BigDecimal rechargeAmount;

效果如下圖所示:

注意事項:

1)@Digits注解識別不了欄位值為null的場景:

2)如果將@Digits注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外:

@Digits(integer = 6, fraction = 2, message = "充值金額只允許6位整數、2位小數")
private Boolean newStudent;

4.6 @Email

作用:被標記的元素必須是郵箱地址,

支持的Java型別:String,

使用示例:

@Email
private String email;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setEmail("活著");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220140414799

上面輸出的message是默認的,在實際使用時可以自定義:

@Email(message = "無效的電子郵件地址")
private String email;

效果如下圖所示:

image-20221220141623447

注意事項:

1)@Email注解識別不了欄位值為null或空字串""的場景:

image-20221220142325648

2)如果將@Email注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.7 @Future

作用:被標記的元素必須為當前時間之后,

支持的Java型別:Date、Calendar、Instant、LocalDate、LocalDateTime、LocalTime等,

使用示例:

@Future
private Date startingDate;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setStartingDate(new Date());

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220143841736

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Future(message = "必須是一個將來的時間")
private Date startingDate;

2)@Future注解識別不了欄位值為null的場景,

3)如果將@Future注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.8 @FutureOrPresent

作用:被標記的元素必須為當前時間或之后,

支持的Java型別:Date、Calendar、Instant、LocalDate、LocalDateTime、LocalTime等,

使用示例:

@FutureOrPresent
private Date startingDate;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setStartingDate(DateUtils.addMilliseconds(new Date(), 1));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220145520752

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@FutureOrPresent(message = "必須是一個將來或現在的時間")
private Date startingDate;

2)@FutureOrPresent注解識別不了欄位值為null的場景,

3)如果將@FutureOrPresent注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.9 @Past

作用:被標記的元素必須為當前時間之前,

支持的Java型別:Date、Calendar、Instant、LocalDate、LocalDateTime、LocalTime等,

使用示例:

@Past
private Date latestAttendanceTime;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setLatestAttendanceTime(DateUtils.addMinutes(new Date(), 10));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220150626760

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Past(message = "必須是一個過去的時間")
private Date latestAttendanceTime;

2)@Past注解識別不了欄位值為null的場景,

3)如果將@Past注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.10 @PastOrPresent

作用:被標記的元素必須為當前時間或之前,

支持的Java型別:Date、Calendar、Instant、LocalDate、LocalDateTime、LocalTime等,

使用示例:

@PastOrPresent
private Date latestAttendanceTime;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setLatestAttendanceTime(DateUtils.addMinutes(new Date(), 10));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220151459339

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@PastOrPresent(message = "必須是一個過去或現在的時間")
private Date latestAttendanceTime;

2)@PastOrPresent注解識別不了欄位值為null的場景,

3)如果將@PastOrPresent注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.11 @Max

作用:被標記的元素必須小于或等于指定的值,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、String,

使用示例:

@Max(value = https://www.cnblogs.com/zwwhnly/archive/2022/12/22/10000)
private BigDecimal balance;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setBalance(new BigDecimal("10000.01"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220152359301

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Max(value = https://www.cnblogs.com/zwwhnly/archive/2022/12/22/10000, message ="必須小于或等于10000")
private BigDecimal balance;

2)@Max注解識別不了欄位值為null的場景,

3)如果將@Max注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.12 @Min

作用:被標記的元素必須大于或等于指定的值,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、String,

使用示例:

@Min(value = https://www.cnblogs.com/zwwhnly/archive/2022/12/22/5000)
private BigDecimal rechargeAmount;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("4999"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220155849229

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Min(value = https://www.cnblogs.com/zwwhnly/archive/2022/12/22/5000, message ="必須大于或等于5000")
private BigDecimal rechargeAmount;

2)@Min注解識別不了欄位值為null的場景,

3)如果將@Min注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.13 @Negative

作用:被標記的元素必須是負數,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、float、Float、

double、Double,

使用示例:

@Negative
private BigDecimal rechargeAmount;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("0"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220171024124

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Negative(message = "金額必須是負數")
private BigDecimal rechargeAmount;

2)@Negative注解識別不了欄位值為null的場景,

3)如果將@Negative注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.14 @NegativeOrZero

@NegativeOrZero注解和@Negative注解基本一致,唯一的區別是被標記的元素除了可以是負數,也可以是零,

使用示例:

@NegativeOrZero(message = "金額必須是負數或零")
private BigDecimal rechargeAmount;

4.15 @Positive

作用:被標記的元素必須是正數,

支持的Java型別:BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、float、Float、

double、Double,

使用示例:

@Positive
private BigDecimal rechargeAmount;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("0"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220173146103

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Positive(message = "充值金額必須是正數")
private BigDecimal rechargeAmount;

2)@Positive注解識別不了欄位值為null的場景,

3)如果將@Positive注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.16 @PositiveOrZero

@PositiveOrZero注解和@Positive注解基本一致,唯一的區別是被標記的元素除了可以是正數,也可以是零,

使用示例:

@PositiveOrZero(message = "充值金額必須是正數或零")
private BigDecimal rechargeAmount;

4.17 @Null

作用:被標記的元素必須為null,

支持的Java型別:Object,

使用示例:

@Null
private String namePinYin;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setNamePinYin("zhangsan");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220174738949

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Null(message = "姓名拼音必須為null")
private String namePinYin;

4.18 @NotNull

作用:被標記的元素必須不為null,

其余和@Null注解一致,

4.19 @NotEmpty

作用:被標記的元素不為null,且不為空(字串的話,就是length要大于0,集合的話,就是size要大于0),

支持的Java型別:String、Collection、Map、Array,

使用示例:

/**
 * 姓名
 */
@NotEmpty
private String name;

/**
 * 家長資訊
 */
@NotEmpty
private List<ParentVO> parentVOList;

ParentVO如下所示:

@Data
public class ParentVO {
    /**
     * 姓名
     */
    @NotEmpty(message = "姓名不能為空")
    private String name;

    /**
     * 手機號
     */
    @NotEmpty(message = "手機號不能為空")
    private String mobile;
}

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setName("");
studentVO.setParentVOList(new ArrayList<>());

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220181939012

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

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

2)如果將@NotEmpty注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

3)嵌套驗證問題

簡單修改下上面的驗證代碼:

StudentVO studentVO = new StudentVO();
studentVO.setName("張三");

ParentVO parentVO = new ParentVO();
studentVO.setParentVOList(Lists.newArrayList(parentVO));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

此時的輸出結果如下所示:

image-20221220183222130

從輸出結果可以看出,StudentVO里增加的@NotEmpty注解生效了,但嵌套的ParentVO里的校驗注解并未生效,如果想生效的話,需要加上@Valid注解:

/**
 * 家長資訊
 */
@Valid
@NotEmpty
private List<ParentVO> parentVOList;

再次執行上面的驗證代碼,輸出結果如下圖所示:

image-20221220183913587

可以看出,嵌套的ParentVO里的校驗注解也生效了,

4.20 @NotBlank

作用:被標記的元素不為null,且必須有一個非空格字符,

這里提下和@NotEmpty的區別,

作用于字串的話,@NotEmpty能校驗出null、”“這2種場景,而@NotBlank能校驗出null、”“、” “這3種場景,

作用于集合的話,@NotEmpty支持,但@NotBlank不支持,

支持的Java型別:String,

使用示例:

@NotBlank
private String name;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setName(" ");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221220185653073

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@NotBlank(message = "姓名不能為空")
private String name;

2)如果將@NotBlank注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.21 @Size

作用:被標記的元素長度/大小必須在指定的范圍內(字串的話,就是length要在指定的范圍內,集合的話,就是size要在指定的范圍內),

支持的Java型別:String、Collection、Map、Array,

使用示例:

@Size(min = 2, max = 5)
private String name;

@Size(min = 1, max = 5)
private List<ParentVO> parentVOList;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setName("張三李四王五");
studentVO.setParentVOList(new ArrayList<>());

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221221103331170

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Size(min = 2, max = 5, message = "姓名不能少于2個字符,不能多于5個字符")
private String name;

@Size(min = 1, max = 5, message = "至少添加一位家長資訊,最多不能超過5位")
private List<ParentVO> parentVOList;

2)@Size注解識別不了欄位值為null的場景,

2)如果將@Size注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4.22 @Pattern

作用:被標記的元素必須匹配指定的正則運算式,

支持的Java型別:String,

使用示例:

@Pattern(regexp = "^[1-9]\\d{5}$")
private String postcode;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setPostcode("2000001");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221221105001625

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Pattern(regexp = "^[1-9]\\d{5}$", message = "郵政編碼格式錯誤")
private String postcode;

2)@Pattern注解識別不了欄位值為null的場景,

3)如果將@Pattern注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

5. Hibernate Validator擴展注解

Hibernate Validator除了支持上面提到的22個原生注解外,還擴展了一些注解:

image-20221221145450366

接下來詳細講解幾個常用的,

5.1 @Length

作用:被標記的元素必須在指定的長度范圍內,

支持的Java型別:String,

使用示例:

@Length(min = 2, max = 5)
private String name;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setName("張三李四王五");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221221110257679

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Length(min = 2, max = 5, message = "姓名不能少于2個字符,不能多于5個字符")
private String name;

2)@Length注解識別不了欄位值為null的場景,

3)如果將@Length注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

5.2 @Range

@Range注解相當于同時融合了@Min注解和@Max注解的功能,如下圖所示:

image-20221221113851165

因此它的作用是:被注解的元素必須大于或等于指定的最小值,小于或等于指定的最大值,

它支持的Java型別也和@Min注解和@Max注解一致:

BigDecimal、BigInteger、byte、Byte、short、Short、int、Integer、long、Long、String,

使用示例:

@Range(min = 1000L, max = 10000L)
private BigDecimal rechargeAmount;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("500"));

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221221112728704

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@Range(min = 1000L, max = 10000L, message = "至少充值1000,最多充值10000")
private BigDecimal rechargeAmount;

2)@Range注解識別不了欄位值為null的場景,

3)如果將@Range注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

4)不建議將@Range注解使用在String型別上,

5.3 @URL

作用:被標記的元素必須是一個有效的url地址,

它的內部其實是使用了@Pattern注解,如下圖所示:

image-20221221115137250

因此它支持的Java型別和@Pattern注解一致:String,

使用示例:

@URL
private String url;

驗證:

StudentVO studentVO = new StudentVO();
studentVO.setRechargeAmount(new BigDecimal("1000"));
studentVO.setUrl("url地址");

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

Set<ConstraintViolation<StudentVO>> constraintViolations = validator.validate(studentVO);
for (ConstraintViolation<StudentVO> constraintViolation : constraintViolations) {
    System.out.println(constraintViolation.getMessage());
}

輸出結果:

image-20221221114343660

注意事項:

1)上面輸出的message是默認的,在實際使用時可以自定義:

@URL(message = "無效的url地址")
private String url;

2)@URL注解識別不了欄位值為null的場景,

3)如果將@URL注解使用在不支持的Java型別,程式會拋出javax.validation.UnexpectedTypeException例外,

6. Spring Web專案

如果專案本身是基于Spring Web的,可以使用@ControllerAdvice+@ExceptionHandler來全域處理引數校驗,

首先,新建一個全域例外處理器,并添加@RestControllerAdvice注解:

@RestControllerAdvice
public class GlobalExceptionHandler {
    
}

說明:因為介面回傳的是json,這里使用@RestControllerAdvice等價于同時使用了@ControllerAdvice@ResponseBody

接著,我們將文初的StudentVO修改為:

import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
public class StudentVO {
    /**
     * 姓名
     */
    @NotBlank(message = "姓名不能為空")
    @Length(max = 20, message = "姓名不能超過20個字符")
    private String name;

    /**
     * 年齡
     */
    @NotNull(message = "年齡不能為空")
    private Integer age;
}

然后在api介面的引數前增加@Valid注解:

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("student/add")
    public CommonResponse<Void> add(@RequestBody @Valid StudentVO studentVO) {
        studentService.add(studentVO);

        return CommonResponse.success();
    }
}

6.1 處理MethodArgumentNotValidException例外

在全域例外處理器中添加MethodArgumentNotValidException例外處理邏輯:

/**
 * 處理MethodArgumentNotValidException
 *
 * @param e
 * @return
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
public CommonResponse<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
    log.error("方法引數不正確", e);

    return CommonResponse.error(HttpStatus.BAD_REQUEST.value(),
            "引數錯誤:" + e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}

最后使用postman呼叫介面進行驗證,如下圖所示:

snipaste_20221111_144615

從介面回傳結果,可以看出,全域例外處理器成功的處理了MethodArgumentNotValidException例外的邏輯,因為上面呼叫介面,其實程式是拋出了org.springframework.web.bind.MethodArgumentNotValidException例外,不過因為在全域例外處理器中定義了該例外的處理邏輯,所以程式按照定義的格式回傳給了前端,而不是直接將例外拋給前端:

snipaste_20221111_145918

6.2 處理HttpMessageNotReadableException例外

上面的介面,如果我們不傳引數,程式會拋出org.springframework.http.converter.HttpMessageNotReadableException例外,如下圖所示:

snipaste_20221111_151244

因此需要在全域例外處理器中添加HttpMessageNotReadableException例外處理邏輯:

/**
 * 處理HttpMessageNotReadableException
 *
 * @param e
 * @return
 */
@ExceptionHandler(HttpMessageNotReadableException.class)
public CommonResponse<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
    log.error("引數錯誤", e);

    return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), "引數錯誤");
}

使用postman呼叫介面進行驗證,如下圖所示:

snipaste_20221111_151748

6.3 處理MissingServletRequestParameterException例外

假設我們有一個根據名字查詢學員的GET請求的介面:

@GetMapping("student/get")
public CommonResponse<StudentVO> get(@RequestParam String name) {
    StudentVO studentVO = studentService.getByName(name);

    return CommonResponse.success(studentVO);
}

但呼叫時,我們不傳遞引數name,程式會拋出org.springframework.web.bind.MissingServletRequestParameterException例外,如下圖所示:

snipaste_20221111_164107

因此需要在全域例外處理器中添加MissingServletRequestParameterException例外處理邏輯:

/**
 * 處理MissingServletRequestParameterException
 *
 * @param e
 * @return
 */
@ExceptionHandler(MissingServletRequestParameterException.class)
public CommonResponse<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
    log.error("引數錯誤", e);

    return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), "引數錯誤");
}

使用postman呼叫介面進行驗證,如下圖所示:

snipaste_20221111_164216

6.4 處理ConstraintViolationException例外

還是上面的查詢學員介面,不僅要傳引數name,還得保證引數name不能是個空字串,因此需要在引數前加上@NotBlank注解:

@GetMapping("student/get")
public CommonResponse<StudentVO> get(@RequestParam @NotBlank(message = "名字不能為空") String name) {
    StudentVO studentVO = studentService.getByName(name);

    return CommonResponse.success(studentVO);
}

并且需要在控制器Controller上添加@Validated注解:

snipaste_20221111_165935

注意事項:控制器上的@Validated注解一定要添加,否則引數上加的@NotBlank注解不會生效,

此時呼叫介面,但引數name傳遞個空字串,程式會拋出javax.validation.ConstraintViolationException例外,如下圖所示:

snipaste_20221111_165205

因此需要在全域例外處理器中添加ConstraintViolationException例外處理邏輯:

/**
 * 處理ConstraintViolationException
 *
 * @param e
 * @return
 */
@ExceptionHandler(ConstraintViolationException.class)
public CommonResponse<Void> handleConstraintViolationException(ConstraintViolationException e) {
    log.error("引數錯誤", e);

    return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), e.getConstraintViolations().iterator().next().getMessage());
}

使用postman呼叫介面進行驗證,如下圖所示:

snipaste_20221111_170610

6.5 擴展

全域例外處理器除了處理上面提到的4個引數校驗的例外,一般也會處理業務上拋出的例外,如Service層拋出的自定義例外:

@Service
public class StudentService {
    public StudentVO getByName(String name) {
        throw new ServiceException("學員不存在");
    }
}
/**
 * 業務例外
 */
public class ServiceException extends RuntimeException {
    public ServiceException(String message) {
        super(message);
    }
}

所以一般全域例外處理器中都有處理ServiceException的邏輯:

/**
 * 處理ServiceException
 *
 * @param e
 * @return
 */
@ExceptionHandler(ServiceException.class)
public CommonResponse<Void> handleServiceException(ServiceException e) {
    log.error("業務例外", e);

    return CommonResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}

因為例外有很多種型別,而本文中提到的只是其中的幾個,因此為了起到兜底作用,可以在全域例外處理器中添加處理Exception例外的邏輯,當程式拋出未知的例外時,可以統一處理,回傳某個固定的提示給前端:

/**
 * 處理Exception
 *
 * @param e
 * @return
 */
@ExceptionHandler(Exception.class)
public CommonResponse<Void> handleException(Exception e) {
    log.error("系統例外", e);

    return CommonResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失敗,請稍后重試");
}

6.6 完整的GlobalExceptionHandler代碼

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

/**
 * 全域例外處理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 處理MethodArgumentNotValidException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResponse<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("方法引數不正確", e);

        return CommonResponse.error(HttpStatus.BAD_REQUEST.value(),
                "引數錯誤:" + e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 處理HttpMessageNotReadableException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public CommonResponse<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("引數錯誤", e);

        return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), "引數錯誤");
    }

    /**
     * 處理MissingServletRequestParameterException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public CommonResponse<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("引數錯誤", e);

        return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), "引數錯誤");
    }

    /**
     * 處理ConstraintViolationException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public CommonResponse<Void> handleConstraintViolationException(ConstraintViolationException e) {
        log.error("引數錯誤", e);

        return CommonResponse.error(HttpStatus.BAD_REQUEST.value(), e.getConstraintViolations().iterator().next().getMessage());
    }

    /**
     * 處理ServiceException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public CommonResponse<Void> handleServiceException(ServiceException e) {
        log.error("業務例外", e);

        return CommonResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    /**
     * 處理Exception
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public CommonResponse<Void> handleException(Exception e) {
        log.error("系統例外", e);

        return CommonResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失敗,請稍后重試");
    }
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/540503.html

標籤:其他

上一篇:時間老去,Ruby不死,Ruby語言基礎入門教程之Ruby3全平臺開發環境搭建EP00

下一篇:基于 Dubbo-Admin 實作根據請求條件路由

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more