In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to solve the problem of invalid @Valid,@Validated,@NotNull annotation when quickly verifying entity classes". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn together "how to solve the problem of invalid @Valid,@Validated,@NotNull annotation when quickly verifying entity classes"!
directory
Verification entity class parameter content cannot be empty. If comment verification is used, invalid verification.
Use @valid annotation to introduce dependencies first
1, replacement method into the reference type
2. Too many errors
3. Use the object to receive the wrong content and output it according to your own requirements
springboot verification mechanism @Validated @Valid
1. Explore the reasons
2. Use @Validated to implement verification mechanism
3. Use @Valid to implement verification mechanism
Verify entity class parameter content cannot be empty. Use annotation to verify invalid. Use @valid annotation to introduce dependency first.
If it is a SpringBoot project, the introduction of web development packages, there is no need to introduce a separate @valid dependency, because it exists in the core of the Web development package.
org.springframework.boot spring-boot-starter-web 2.0.5.RELEASE
If not SpringBoot project, explicitly introduce @valid dependency in Pom of Maven
javax.validation validation-api 1.1.0.Final org.hibernate hibernate-validator 5.4.1.Final
Verification entity class Most people use methods that must be taken out of the controller layer to determine whether it is empty. In fact, you can directly verify it through the annotation @NotNull in the entity class, as shown in the figure.
But after my experiment, do not know what reason, in my project did not play a role, solve as follows
1, replacement method into the reference type
@Valid can be used in methods, constructors, method parameters, and member attributes (fields). At first, I used json to receive it in the controller layer, and then verified it after converting it to an entity class, as shown in the figure.
Modify as follows: when controller receives data, directly receive it as an object, and directly check it with comments in the entry
2. Too many errors
At this time, the object field verification takes effect, but when an error occurs, the returned content is very messy, as shown in the figure:
3. Use the object to receive the wrong content and output it according to your own requirements
As can be seen from the above picture, defaultMessage in the returned content is the content you want to output, so add an object of type BindingResult that receives error information to the method. If the object has data, output error data, as shown in the figure:
Another level of implementation of the entity class parameter verification, if you have a better way to leave a message
springboot verification mechanism@Validated@Valid1, explore the reason
The usage of @Validated and @Valid has been confused throughout development, sometimes @Validated, sometimes @Valid. Although verification can be achieved, it is still not clear when it will take effect, and it is not known how it will take effect.
First locate the package to which the two annotations belong:
@Validated is the core package provided by spring under spring-context package
@Valid under validation-api package version 2.0.2
@Validated is the core package of spring, which is something that every project has, so how is api introduced? View maven dependencies
This dependency was introduced when Spring-boot-start-web was introduced.
The two annotations have different packages, and where do @NotNull ,@Null ,@Size,@Max check annotations come from?
These comments are all under the api package
2. Use @Validated to implement verification mechanism
Scenario 1: The query parameter is an entity, and the Get request is normal without adding any comments. The entity parameter fields are null.
Now the requirement id field cannot be empty. Mark @NotNull in the entity id field. Continue to query and find that the annotation is not effective.
After testing, only @Validated before the request entity parameter list will take effect, even if @Validated is added to the class, it will not take effect
Scenario 2: Query parameter is a basic or reference type field. Add @NutNull to the parameter list to modify this field. Discovery does not take effect
Tested: Only adding @Validated to global class will take effect, even adding @Validated to parameter list will not take effect
Exception generation is also different: in the case of validation, the exception generated by entity class validation is: BindException , while the exception generated by parameter list is:ConstraintViolationException
3. Use @Valid to implement verification mechanism
Scenario 1: Same as above, only @Valid will take effect before the parameter list
Scenario 2:@Valida doesn't work either on the parameter list or on the class. Only @Validated global settings can be used
Conclusion: It is not clear why @Valid design appears, so verification @Validate can be realized.
Additional global exception catch:
@RestControllerAdvicepublic class GlobalException { @ExceptionHandler({BindException.class}) public RespResult validationException(BindException exception){ List errors = exception.getAllErrors(); if(! CollectionUtils.isEmpty(errors)){ StringBuilder sb = new StringBuilder(); errors.forEach(e->sb.append(e.getDefaultMessage()).append(",")); return new RespResult(400, sb.toString()); } return new RespResult(500, exception.getLocalizedMessage()); } @ExceptionHandler({ConstraintViolationException.class}) public RespResult constraintViolationException(ConstraintViolationException exception){ Set
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.