Author: your sweet Source: juejin. Cn/post / 6913735652806754311

Some time ago, when submitting code review, my colleague mentioned a defect in code specification: parameter verification should be placed in the Controller layer. How should do parameter check in the end

Controller vs. Service layer

I went to the Internet to check some information. Generally, it is recommended that those not related to business should be checked in the Controller layer, while those related to business should be checked in the Service layer.

So how do I make the parameter validation look nice? If it’s all if-else, I feel like I’m writing low code, but I’m glad I have wheels

Common verification tool classes

Using Hibernate Validate

Introduction of depend on

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> < version > this Final < / version > < / dependency >Copy the code

Common notes

Use the pose

The recommendation of a Spring foundation of the Boot will not introduced, the actual combat tutorial: www.javastack.cn/categories/…

Use it together with the @validated or @VALID annotation in Controller. There is not much difference between the @validated and @VALID annotation. In general, you can select either annotation as follows:

Although @ is more powerful than @Valid and provides grouping and verification sorting functions on top of @Valid, it has not been used in actual projects

Annotations in the Hibernate-validate framework need to be used with entities

  • Define an entity
Public class DataSetSaveVO {// The unique identifier is blank @notBlank (message = "user uuid is empty") // The user name can only be letters and numbers @pattern (regexp =) "^[a-z0-9]+$", message = "user names can only be alphabetic and numeric") @Length(max = 48, message = "user uuid length over 48 byte") private String userUuid; / / data set name can only be letters and Numbers @ the Pattern (regexp = "^ / A - Za - z0-9 + $". Message = "data set names can only be letters and Numbers") @length (Max = 48, Message = "file name too long") @notBlank (message = "file name is empty") private String name; // Data set description is at most 256 bytes @Length(Max = 256, Message = "data set description length over 256 bytes ") @notblank (message = "data set description is null") private String description; }Copy the code

Note: The message field is the exception that is thrown when the verification rule is not met

  • Methods in the Controller layer
@PostMapping
public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {
    return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));
}
Copy the code

Note: Add the @VALID or @validated annotation next to the Validated entity DataSetSaveVO

Use the Commons – lang3

Introduction of depend on

Mons < dependency > < groupId > org.apache.com < / groupId > < artifactId > Commons - lang3 < / artifactId > < version > 3.4 < / version > </dependency>Copy the code

Common Methods

The test code

//StringUtils.isEmpty
System.out.println(StringUtils.isEmpty(""));  //true
System.out.println(StringUtils.isEmpty("  "));  //false
//StringUtils.isNotEmpty
System.out.println(StringUtils.isNotEmpty(""));  //false
        
//StringUtils.isBlank
System.out.println(StringUtils.isBlank(""));  //true
System.out.println(StringUtils.isBlank(" "));  //true
//StringUtils.isNotBlank
System.out.println(StringUtils.isNotBlank(" "));  //false

List<Integer> emptyList = new ArrayList<>();
List<Integer> nullList = null;
List<Integer> notEmptyList = new ArrayList<>();
notEmptyList.add(1);

//CollectionUtils.isEmpty
System.out.println(CollectionUtils.isEmpty(emptyList));   //true
System.out.println(CollectionUtils.isEmpty(nullList));   //true
System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false

//CollectionUtils.isNotEmpty
System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false
System.out.println(CollectionUtils.isNotEmpty(nullList));   //false
System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true
Copy the code

Custom annotations

When none of the above is sufficient for validation, consider using custom annotations.

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!

3. Oh, my gosh! What new syntax is xx ≠ null in Java?

4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!