Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring Validation

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Spring Validation". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Spring Validation.

First add POM dependencies

Create a new Spring Boot project and add dependencies in the pom.xml file:

One thing to pay special attention to is that if spring-boot-starter-web dependencies are introduced into our project, it will automatically rely on spring-boot-starter-validation, eliminating the need for us to manually add dependencies to prevent some version compatibility problems.

Add constraint comment

Add constraint comments to the attributes corresponding to the UserReq entity class that receives the parameters:

Note:

Each constraint annotation has a message element that is used to validate the message if it fails.

Multiple constraint annotations can be added to an attribute. All annotations are related to and must be verified.

You can use regular expressions to verify parameters

Sharp-eyed students may find that some of the constraint annotations used in the above code are referenced under the javax.validation.constraints package and some from the org.hibernate.validator.constraints package.

At this point, I have to mention that JSR-303,JSR-303 is a subspecification of JAVA EE (now renamed: Jakarta EE), called Bean Validation, which defines a number of entity and method validation constraints and interface specifications, while Hibernate Validator provides implementation for all built-in constraint annotations in the Bean Validation specification, and adds some additional constraint annotations.

Constraint annotations built into Bean Validation:

Constraint annotations added in Hibernate Validator:

It is worth mentioning that some excellent constraint annotations added in Hibernate Validator, such as @ Email, @ NotEmpty, @ NotBlank, have been absorbed into the Bean Validation standard constraint annotations, so we should try our best to use the constraint annotations under the javax.validation.constraints package when Import in our code.

How do I use it?

We have modified the entity class that receives parameters, but we still cannot achieve the function of verification. We still need to modify Controller:

Add @ Validated annotation before the parameters of the receiving method, or you can add @ Valid annotation.

To obtain the result of parameter verification, the parameter to be checked needs to be followed by a parameter of type BingingResult, which is used to bind the verification result.

With regard to the first point, the @ Validated annotation is provided by the Spring framework, which can be said to be an encapsulation of the JSR-303 specification standard annotation @ Valid, which can provide additional functions such as grouping checking. In fact, any annotation that starts with "Valid" can achieve the same effect (as for the reason, if you have the opportunity to write a special article, you can follow it if you are interested)

As for the second point, why the verification result is automatically bound to the following BingingResult object, which involves the knowledge related to Spring DataBinder data binding (which will be explained later, please follow if you are interested), now we just need to know how to write it.

Request test

Now we use the Postman tool to request our interface:

Then we hit the breakpoint to debug the bindingResult object:

You can see that the bindingResult object was successfully bound and returned the result of three property validation failures.

Packet check

There is usually a situation when we write business: we usually do not need to verify parameter Id when adding new operations, and we need to verify parameter Id when modifying or deleting operations, so what should we do with the same parameter receiving class UserReq?

It's very simple. First of all, let's create an update grouping. All we need is an ordinary interface class:

Then we add an id attribute to the UserReq class, along with the @ NotNull grouping check:

Groups supports passing in an array, which can be passed in multiple groups, and for the id attribute, we need to validate it during both update and delete operations.

Finally, add a check packet to the update interface method in Controller:

We just need to add the Update grouping that needs to be verified in the comments.

At this point, I believe you have a deeper understanding of "how to use Spring Validation". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report