Author: frantic childe cnblogs.com/cjsblog/p/8946768.html

1. Background

In the development process, background parameter verification is essential, so you will often see code like the following.

There’s nothing wrong with it, it’s neat, it just doesn’t look very elegant.

Next, use Validation to rewrite this paragraph.

Validation in the Spring Boot document

Validation is simply mentioned on the Spring Boot website as follows

Spring Validator and Hibernate Validator are two sets of Validators that can be mixed up. Hibernate Validator is used in this case. Reply keyword: _boot_ in the background of wechat official account of Java technology stack, you can obtain more dry goods of Spring Boot series technology with stack length sorted.

3, Hibernate Validator

Docs.jboss.org/hibernate/s…

Four, Spring the Validator

Docs. Spring. IO/spring/docs…

5, sample

5.1. Introduce spring-boot-starter-validation

5.2. Define an object

5.3. The @valid verification is applied and the verification result is put into the BindingResult object

Recommended reading: Verifying the difference between @validated and @valid.

Note:

  • By default, if check failure can throw javax.mail validation. ConstraintViolationException is unusual, can use unified exception handling to do processing of these abnormal

  • An Errors/BindingResult argument is expected to be declared immediately after the model attribute

Recommended reading: Verifying the difference between @validated and @valid.

5.4, see the effect

If you add a Model object after the checksum object, if you return a ModelAndView, you can set the Model to it and get an error message on the page

It is too inflexible to check only single fields. If there is correlation between fields, how to check? The answer is customization.

5.5. Customize verification rules

Docs.jboss.org/hibernate/s…

Here, to create a coupon as an example to demonstrate how to customize the verification rules

First, the coupon form looks like this (for demonstration purposes only) :

In addition to the two custom validation rules, grouping is also used.

Why are there groups? Because, for example, when you add, you don’t need to validate the ID, and when you change the ID, you can validate group A when you add, and validate group B when you change

Let’s focus on @checktimeInterval

The first step is to define an annotation called CheckTimeInterval

Second, define the Validator to validate it

By the way, here BeanWrapper is going to fetch the property value of the object, so let’s take a look a little bit at what BeanWrapper does.

 

Get to the point

Step 3: Verify

 

Look, the custom validation is working.

6, add

6.1. Verification mode

Docs.jboss.org/hibernate/s…

One more thing about the validation mode.

By default, all attributes are checked and the error message is returned, but in many cases this is not necessary. If one verification fails, the others are not checked. To do this, you need to set it like this, okay?

6.2 Verification of a single parameter

If the parameter verification fails during page adjustment, you can switch to the error page instead of processing the error.

If the interface parameter verification fails, you can perform unified processing here and return. Such as:

6.3 error page

Take, for example, the coupon details

http://localhost:8080/coupon/detail.html

400

http://localhost:8080/coupon/detail.html?id=

400

http://localhost:8080/coupon/detail.html?id=abc

400

http://localhost:8080/coupon/detail222.html?id=123

404

Without permission

403

int a = 1 / 0;

500

6.4. @Valid and @Validated

Blog.csdn.net/qq_27680317…

reference

  • Rensanning.iteye.com/blog/235737…

  • Blog.csdn.net/kenight/art…

  • www.cnblogs.com/mr-yang-loc…

  • www.phpsong.com/3567.html

  • www.cnblogs.com/mr-yang-loc…

Read more on my blog:

1.Java JVM, Collections, Multithreading, new features series tutorials

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Java, backend, architecture, Alibaba and other big factory latest interview questions

Life is good. See you tomorrow