![](/img/trans.png)
[英]Is it possible to combine spring boot @Value with javax.validation.constraints?
[英]javax validation constraints not working in Spring Boot
我無法在 Spring Boot 應用程序中觸發 @NotEmpty、@NotBlank 和 @NotNull 等注釋。
我遵循了這個(Maven)示例:
https://spring.io/guides/gs/validating-form-input/
......我不明白我做錯了什么。
POJO:
import javax.validation.constraints.NotBlank;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{
@NotBlank
private String collegeCode;
.
.
.
彈簧控制器:
@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);
}
屏幕:
<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>
.
.
.
除了@NotBlank,我也嘗試過@NotEmpty 和@NotNull,但同樣的事情發生了。 我的屏幕不驗證輸入,並允許保存帶有空大學代碼的大學對象。
有趣的是,如果我將 POJO 更改為使用已棄用的 Hibernate 驗證器...
import org.hibernate.validator.constraints.NotBlank;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{
@NotBlank
private String collegeCode;
.
.
.
...然后驗證會觸發,並且屏幕阻止我使用空的 CollegeCode 保存 College 對象。
誰能告訴我為什么我的驗證在使用 javax.validation.constraints 驗證器時不起作用?
從 2.3.0.RELEASE 版本開始,驗證啟動器默認不包含在 web/webflux 啟動器中,您需要手動添加它。 更多細節在這里
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
在 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>
我想我已經想通了這個問題。 我遇到了同樣的問題,並試圖降低 Spring Boot 版本。 我將 Spring Boot 版本從 2.3.0.RELEASE 降低到 2.2.7.RELEASE 並且它有效)也許這個答案會對某人有所幫助。
請將 bean MethodValidationPostProcessor 添加到您的上下文中
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
有完全相同的問題,不知道該怎么辦。 所有需要的類都在類路徑中,沒有 jar 地獄等。
I m using spring boot 2.3.12 the contract first api is generate with swagger 3.0.0:
這是一個生成的屬性:
@ApiModelProperty(required = true, value = "")
@NotNull
public String getEmail() {
return email;
}
這是我使用的依賴項:
<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.