In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to standardize the use of @ Valid annotations". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to standardize the use of @ Valid annotations" can help you solve the problem.
@ Valid annotations and usage specification annotations description @ AssertFalse annotated elements must be false, boolean/Boolean@AssertTrue annotated elements must be true, boolean/Boolean@DecimalMax annotated elements must be a number, and its value must be less than or equal to the specified maximum value @ DecimalMin annotated element must be a number Its value must be greater than or equal to the specified minimum value @ Digits annotated element must be a number within an acceptable range @ Future annotated element must be at some point in the future, date or time @ Max annotated element must be a number, its value must be less than or equal to the specified maximum value @ Min annotated element must be a number Its value must be greater than or equal to the specified minimum value @ NotNull annotated element cannot be Null@Null annotated element must be Null@Past annotated element must be some time, date or time in the past @ Pattern annotated element must conform to the specified regular expression @ Size annotated element must be greater than or equal to the specified minimum value Less than or equal to the specified maximum value @ Email annotated element must be a well-formed email address @ NotEmpty annotated element cannot be empty, String type cannot be null,Array, Map cannot be empty, cut size/length greater than 0@NotBlank string cannot be empty, empty string, full space @ URL string must be a URL@Valid annotation specification user request parameter business scenario
For a user's registration operation (Post request), it often involves the Post submission of account (username) and password (password):
/ / user sends POST request to create new user @ PostMappingpublic User create (@ RequestBody User user) {/ * * some data persistence operations, such as: write to database * * / / print information submitted by user System.out.println (user.getId ()); System.out.println (user.getUsername ()); System.out.println (user.getPassword ()); System.out.println (user.getBirthday ()) Problems with return user;} Business
However, users often accidentally submit an empty password to register, which is not allowed, so we often need to make an empty judgment on the password information submitted by the user. The common method is to directly determine the null of the if statement:
/ / the user sends a POST request to create a new user @ PostMappingpublic User create (@ RequestBody User user) {if (StringUtils.isBlank (user.getPassword () {/ / the password entered by the user is empty, and perform exception handling} / * * some data persistence operations, such as: write to the database * / / print the information submitted by the user System.out.println (user.getId ()) System.out.println (user.getUsername ()); System.out.println (user.getPassword ()); System.out.println (user.getBirthday ()); return user;}
The above methods seem to work, but once there are more Post methods, you need to if each Post request to determine whether it is empty or not, the code becomes redundant, and once you modify one place, all if statements need to be modified, and the maintainability becomes poor.
Optimized solution
So is there a way to do it once and for all without a lot of code redundancy and good maintainability? Then the @ Valid annotation under the javax.validation package comes in handy.
1. First, we add the @ NotBlank annotation (hibernate.validator.constraints package) to the password attribute in the entity class User.java
Import org.hibernate.validator.constraints.NotBlank;public class User {public interface UserSimpleView {} public interface UserDetailView extends UserSimpleView {} private String username; / / add a NotBlank non-null constraint to this attribute @ NotBlank private String password; private String id; private Date birthday; public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this.birthday = birthday @ JsonView (UserSimpleView.class) public String getId () {return id;} public void setId (String id) {this.id = id;} @ JsonView (UserSimpleView.class) public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} @ JsonView (UserDetailView.class) public String getPassword () {return password } public void setPassword (String password) {this.password = password;}}
two。 We annotate @ Valid in the parameters of the Post method of the Controller class, and use BindingResult to print the error message as a log to the background
@ PostMappingpublic User create (@ Valid @ RequestBody User user, BindingResult errors) {if (errors.hasErrors ()) {/ / exception handling errors.getAllErrors () .stream () .forEach (error-> System.out.println (error.getDefaultMessage ();} user.setId ("1"); System.out.println (user.getId ()); System.out.println (user.getUsername ()) System.out.println (user.getPassword ()); System.out.println (user.getBirthday ()); return user;}
3. At this point, when we submit empty password information to the server Post, an error message will be printed in the background:
May not be empty
This is the end of the content about "how to standardize the use of @ Valid annotations". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.