In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "how to use the annotations @ Validated and BindingResult for non-empty verification of input parameters", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the annotations @ Validated and BindingResult to check the input parameters in a non-empty way.
Catalogue
Note @ Validated and BindingResult to check the input parameter is not null
@ Validated and BindingResult use the pit encountered
Note @ Validated and BindingResult to check the input parameter is not null
Input parameter verification is necessary in the project, and the server and browser do not trust each other. It is wrong not to deal with the background because the front-end join the parameter and cut off the background.
For example, an object is passed from the foreground as an input parameter. Some properties in this object are allowed to be empty, and some properties are not allowed to be empty. So are you still using if () else {} to make non-null judgments? You might as well try annotations, using @ Validated and BindingResult.
Note: BindingResult needs to be placed after @ Validated
The sample code is as follows: ordinary properties can use @ NotEmpty () special use @ NotNull () for example: enumerated types
Entity class: add what is not allowed to be empty
@ NotEmpty (groups = {SchoolDTO.SchoolGroup.class}, message = "School name cannot be empty")
Note: tell me twice about important things. Public interface SchoolGroup {} Don't forget to add this! Public interface SchoolGroup {} Don't forget to add this!
Public class SchoolDTO implements Serializable {private Long id; @ NotEmpty (groups = {SchoolDTO.SchoolGroup.class}, message = "School name cannot be empty") private String schoolName; @ NotNull @ NotEmpty (groups = {SchoolDTO.SchoolGroup.class}, message = "School administrators cannot be empty") private String schoolPrincipal; @ NotNull @ NotEmpty (groups = {SchoolDTO.SchoolGroup.class}, message = "Job cannot be empty") private String principalPosition @ NotNull @ NotEmpty (groups = {SchoolDTO.SchoolGroup.class}, message = "contact number cannot be empty") private String schoolPhone; / / getter setter tostring omit public interface SchoolGroup {} Don't forget to add this}
Control layer: add @ Validated ({SchoolDTO.SchoolGroup.class}) and BindingResult bindingResult @ Validated to the input object for verification. BindingResult can get the verification error information.
@ PostMapping ("/ schools") @ PreAuthorize ("hasRole (\" + AuthoritiesConstants.ADMIN + "\")) public Map createSchool (@ RequestBody @ Validated ({SchoolDTO.SchoolGroup.class}) SchoolDTO schoolDTO,BindingResult bindingResult) throws Exception {/ / returns the verification error message Mapmap=new HashMap (); if (bindingResult.hasErrors ()) {map.put ("success", "false") Map.put ("message", bindingResult.getAllErrors ()); return map;} /. Business omitting return map;}}
Test: I did not pass principalPosition and schoolPhone when I entered the parameter.
@ Validated and BindingResult use the pit encountered
@ Validated and BindingResult need to be adjacent, otherwise the variable result cannot accept error messages
Console output
Field error in object 'entity' on field' variable: rejected value [null]; codes [NotNull.entity. Variable, NotNull. Variable, NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes
Screenshot of the correct content
The above is my personal experience. I hope I can give you a reference and I hope you can give me more support.
At this point, I believe you have a deeper understanding of "how to use the annotations @ Validated and BindingResult for non-empty verification of input parameters". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.