[英]javax validation constraints not working in Spring Boot
I cannot get annotations such as @NotEmpty, @NotBlank and @NotNull to fire in my Spring Boot application.我无法在 Spring Boot 应用程序中触发 @NotEmpty、@NotBlank 和 @NotNull 等注释。
I've followed this (maven) example:我遵循了这个(Maven)示例:
https://spring.io/guides/gs/validating-form-input/ https://spring.io/guides/gs/validating-form-input/
...and I don't see what I am doing wrong. ......我不明白我做错了什么。
POJO: POJO:
import javax.validation.constraints.NotBlank;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{
@NotBlank
private String collegeCode;
.
.
.
Spring controller:弹簧控制器:
@RequestMapping(value="/addCollege", method = RequestMethod.POST)
public String addCollege(@Valid @ModelAttribute("college") College college, BindingResult bindingResult, Model model, HttpSession session)
{
if(bindingResult.hasErrors()) //this is never true
{
logger.debug("Errors when adding new college!!!");
return "admin/colleges/addCollege";
}
collegeProcessor.saveCollege(college);
return getAllColleges(model, session);
}
Screen:屏幕:
<form action="#" th:action="${addOrEdit} == 'add' ? @{/addCollege} : @{/updateCollege}" th:object="${college}" method="post">
<table class="table">
<tr>
<td>College Code</td>
<td><input size="10" name="collegeCode" th:field="*{collegeCode}"/></td>
<div id="errors" th:if="${#fields.hasErrors('collegeCode')}" th:errors="*{collegeCode}"></div>
</tr>
.
.
.
As well as @NotBlank, I have tried @NotEmpty and @NotNull, but the same thing happens.除了@NotBlank,我也尝试过@NotEmpty 和@NotNull,但同样的事情发生了。 My screen does not validate the input, and allows a college object with an empty collegeCode to be saved.
我的屏幕不验证输入,并允许保存带有空大学代码的大学对象。
The interesting thing is if I change my POJO to use the deprecated Hibernate validator instead...有趣的是,如果我将 POJO 更改为使用已弃用的 Hibernate 验证器...
import org.hibernate.validator.constraints.NotBlank;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{
@NotBlank
private String collegeCode;
.
.
.
...then the validation DOES fire, and the screen prevents me from saving a College object with an empty collegeCode. ...然后验证会触发,并且屏幕阻止我使用空的 CollegeCode 保存 College 对象。
Can anyone tell me why my validation doesn't work when using the javax.validation.constraints validators?谁能告诉我为什么我的验证在使用 javax.validation.constraints 验证器时不起作用?
Since version 2.3.0.RELEASE the validation starter is not included in web/webflux starters by default, you need to add it manually.从 2.3.0.RELEASE 版本开始,验证启动器默认不包含在 web/webflux 启动器中,您需要手动添加它。 More details here
更多细节在这里
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Include Maven dependency for 2.3.0.RELEASE in pom.xml, however, it is not required for lower version of Spring Boot Application.在 pom.xml 中包含 2.3.0.RELEASE 的 Maven 依赖项,但较低版本的 Spring Boot 应用程序不需要。
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
I guess I've figured out this problem.我想我已经想通了这个问题。 I had the same problem and tried to lower spring boot version.
我遇到了同样的问题,并试图降低 Spring Boot 版本。 I lowered my spring boot version from 2.3.0.RELEASE to 2.2.7.RELEASE and it works) Maybe this answer will help someone.
我将 Spring Boot 版本从 2.3.0.RELEASE 降低到 2.2.7.RELEASE 并且它有效)也许这个答案会对某人有所帮助。
Please add a bean MethodValidationPostProcessor to your context请将 bean MethodValidationPostProcessor 添加到您的上下文中
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
having exactly the same issue, no idea what to do.有完全相同的问题,不知道该怎么办。 All needed classes are in the classpath, no jar hells etc.
所有需要的类都在类路径中,没有 jar 地狱等。
I m using spring boot 2.3.12 the contract first api is generate with swagger 3.0.0:
that's an attribute generated:这是一个生成的属性:
@ApiModelProperty(required = true, value = "")
@NotNull
public String getEmail() {
return email;
}
here are the dependencies i used :这是我使用的依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.