java - @NotNull 约束在 Spring Boot 中不起作用
问题描述
我正在使用 Spring Boot 并且 @NotNull 不起作用。当未提供名称值时,该函数运行良好。
@Entity
public class Employee implements Serializable{
@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
//@Id
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@NotNull // not working
private String name;
private String phone;
}
控制器是
@Controller
public class URLController {
@Autowired
EmployeeServiceImpl empService;
@GetMapping({"/", "/index"})
public String index1(Model model){
model.addAttribute("employee",new Employee());
return "index";
}
@PostMapping("/result")
public String result(@Valid @ModelAttribute Employee employee){
System.out.print(employee.getName());
empService.save(employee);
return "result";
}
}
pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0
<groupId>com.springs</groupId>
<artifactId>springs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springs</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我正在使用模型属性来获取员工对象。pom.xml 文件包含休眠验证器依赖项。
解决方案
例如,您必须在代码的映射部分中添加 @Valid 注释
void addEmployee(@Valid Employee emp);
使用此导入“import javax.validation.Valid;”
另一个注意事项,如果你想验证列表,你必须制作一个包装器列表
public class ValidList<E> implements List<E> {
@Valid
private List<E> list;}
并覆盖其中的所有方法
推荐阅读
- wordpress - WPBakery - 在手机上查看页面时文本字体大小很小
- linux - 在 @INC 中找不到 ExtUtils/MakeMaker.pm
- pytorch - 为什么 torch.functional.sigmoid 和 torch.nn.functional.relu 不像 torch.nn.functional.tanh 那样被弃用?
- javascript - 混淆了 promise 和 async/await
- scrapy - 使用带有规则的 start_requests 进行 Scrapy
- ubuntu - MariaDB 使用所有可用的内存
- python - 计算目录和子目录中的文件数
- php - Bootstrap Studio 需要哪些导出设置
- bash - 如何将可能包含引号和非 ASCII 字符的参数从 PowerShell 传递到外部程序?
- c# - 如何在单击按钮时执行 for 循环的每次迭代