java - 验证 bean 在 Java 11 和 spring boot 2.5.3 中不起作用
问题描述
您好,在花费数小时阅读 stackoverflow 答案并试图找到答案之后,我仍然无法找出为什么验证器(例如NotNull
和NotBlank
不能在 java 中工作)。我仍然可以使用空白名称发送帖子请求。该项目在 Java 11 和 Spring boot starter v2.5.3 上。非常感谢你,如果你能弄清楚,我觉得我必须给你买一个冰淇淋:')
在我的模型中:
import javax.validation.constraints.NotBlank;
@NotBlank
private final String name;
在我的控制器中:
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@PostMapping(path="add")
public void addUser(@Valid @NotNull @RequestBody User user) {
//Request body takes the data taken and converts it into a User
userService.addUser(user);
}
@PutMapping(path="user/{id}")
public void updateUser(@PathVariable("id") UUID id, @Valid @NotNull @RequestBody User userToUpdate) {
userService.updateUser(id, userToUpdate);
}
在我的依赖项中pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
解决方案
推荐阅读
- php - 消除
php 文件中的标记以解释 vehicle_overview 中的 html 代码
- css - 在我可以将鼠标悬停在子菜单上之前下拉隐藏
- android - 如何使用 Android Studio 获取 PDF 文件路径?
- c++ - 在 Windows 电源事件期间收集转储
- html - html
样式代码不起作用
- ionic-framework - 如何从 Ionic 3 PWA 调用 HTTP 请求?
- arrays - 将变量数组值转换为vba中的字符串
- redis - 带有redis的气流芹菜 - 6小时后超时
- html - 如何从引导程序 4 中删除下划线和文本悬停
- sql - 如何以 MAX 作为聚合函数获取全记录