Request validation of parameters
When dealing with business logic in a Web project, we often need to validate the request parameters first:
- Take out the parameter value in the code logic, manually judge;
- Use the @valid annotation to let Spring make its own judgment on interface requests.
The return status of method 1 is 200, so you need to customize the return result, which is complicated. Method 2 returns 400, and the return result is obtained from Response Body, which is convenient to use.
@ Valid annotations
Practice is the sole criterion for testing truth
@data public class StudentReq {@notblank (message = "student name cannot be blank!" ) @length (min = 1, Max = 20, message = "Student name maximum 20 characters!" ) private String stuName; @notnull (message = "Student age cannot be empty!" ) @range (min = 1, Max = 100, message = "Student ages Range from 1 to 100!" ) private Integer stuAge; @notempty (message = "How can modern college students have no hobbies!" ) @size (Max = 5, message = "Please pay attention to your study!" ) private List<String> stuHobbys; }Copy the code
How to verify 👉 Link