introduction
When talking about Spring's parameter verification function, the @Validated annotation is undoubtedly an important tool. It provides us with a simple and powerful way to verify the legitimacy of request parameters, ensuring the stability and security of the system. This article will introduce the basic usage of Spring Validated and its application in actual projects.
1. What is validated?
@Validated annotation function and usage method
- It can be used on the Controller class or method to enable the verification function of the requested parameters.
- By using other verification annotations (such as @NotNull, @NotBlank, @Min, @Max, etc.) on method parameters, checksum verification of the requested parameters to ensure the legality of the parameters.
- You can use the Spring Validation API or other verification frameworks that support JSR-303 specifications (such as Hibernate Validator) to maximize the use of @Validated annotation.
Example of practical application of @Validated annotation
- Write a user registration interface and use @Pattern annotation to verify the user name and password.
- Handle exceptions when verification fails and return friendly error messages to the interface caller.
@Validated annotation notes and best practices
- Discuss the techniques of using @Validated annotation in complex verification scenarios.
- How to declare a validator or configure verification rules in a global configuration file, so that the @Validated annotation can be more flexible to adapt to various project needs.
2. Use steps
1.Introduce maven dependencies
The code is as follows (example):
<!-- ValidationLegality verification(parameter) parameter校验框架的依赖--> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
2. Use implementation
The code is as follows: The parameter length is 5 - 16 bits (example):
/** * @Description: User-related control layer * @Author: windStop * @Date: 2024/5/26 16:18 */ @RestController @RequestMapping("/user") @Validated //Enable parameter verification functionpublic class UserController { @Autowired private UserService userService; /** * Used to register a new user. When registering a user, you need to first determine whether the user name exists, because the user name cannot be repeated. * Here I do not intend to add unique fields to the username * @param username * @param password * @return Whether the registration is successful */ @PostMapping("/register") public Result register(@Pattern(regexp = "^\\S{5,16}") String username, @Pattern(regexp = "^\\S{5,16}")String password){ //1. Determine whether the user exists if ((username)){ return ("Username already exists and cannot be registered"); } //2. Register does not exist boolean flag = (username,password); return (flag); } }
Summarize
Through the study of this article, readers will have an in-depth understanding of the usage methods and principles of Spring Validated, and master how to effectively use @Validated annotation for parameter verification in actual projects. I hope this article can provide some inspiration and help for the parameter verification problems you encounter in Spring project development. If you need further information or other questions, please feel free to let me know.
The above is the detailed content of the SpringBoot project using validated to implement the parameter verification framework. For more information about SpringBoot validated parameter verification, please pay attention to my other related articles!