@ Valid validation is invalid BindingResult has not been resolved incorrectly
- 2021-11-30 00:20:38
- OfStack
@ Valid verification failure
Problem description
Validate attribute stuTele in entity class using @ Valid
import javax.validation.constraints.Size;
...
@Size(min = 11,max = 11,message = " Please enter 11 Bit mobile phone number ")
private String stuTele;
In Controller, the @ Valid annotation is used to verify the attribute value of the passed student object
public String updateStuMsg(@Valid Student student, BindingResult bindingResult, Model model, HttpSession httpSession){......}
After passing in the error parameter, it is found that the error recorded in bindingResult is 0, indicating that the verification is invalid
Solving process
Check for imported dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
After switching the version of hibernate-validator to 5.4. 1. Final, the verification was found to be normal
Therefore, it is positioned as a dependent version problem.
My springboot version is 2.5. 1, and the validation takes effect after replacing the dependency with the following
<!-- No version specified here <version> Is used by default and the current springboot The matching version is the 2.5.1 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Go to spring-boot-starter-validation and see its dependencies. There is one
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
<scope>compile</scope>
</dependency>
Therefore, when modifying dependencies externally, you can also use this version 1 directly.
The validation-api dependency may not be retained.
So how to use the latest version of 7.0. 1. Final? That's what the official documents say
Jakarta Bean Validation defines integration points with CDI (context and dependency injection for Jakarta EE). If your application is running in an environment that does not provide this out-of-the-box integration, you can use the Hibernate Validator CDI portable extension by adding the following Maven dependencies to your POM:
Example 1.3: Hibernate Validator CDI Portable Extension Maven Dependency
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>7.0.1.Final</version>
</dependency>
Note that applications running on an Java EE application server typically do not need to add this dependency.
Then, you just need to replace all the previous dependencies with this one, and then you can
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>7.0.1.Final</version>
</dependency>
Use bindingResult for parameter verification
In the control class
@RequestMapping("/create")
public void create(@Valid OrderForm orderForm, BindingResult bindingResult){
if(bindingResult.hasErrors()){
log.error(" "Incorrect Create Order parameter" ,orderForm={}",orderForm);
//bindingResult.getFieldError().getDefaultMessage() You can get an error prompt
throw new OrderException(ResultEnums.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
}
}
Entity class OrderForm
@Data
public class OrderForm {
@NotEmpty(message = " Name required ")
private String name;// Name of Buyer
@NotEmpty(message = " Mobile phone number required ")
private String phone;// Buyer's mobile phone number
@NotEmpty(message = " Address required ")
private String address;// Buyer's address
@NotEmpty(message = "openid Required ")
private String openid;// Buyer WeChat
@NotEmpty(message = " Shopping cart cannot be empty ")
private String items;// Shopping cart
}