validation - 如何在 Spring Boot 中使用 @NotNull?
问题描述
我有这个依赖:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
它的版本由哪个管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
我有这件作品:
import javax.validation.constraints.NotNull;
//other imports ommited
@Component
@ConfigurationProperties(prefix = "collector")
public class CollectorProperties {
@NotNull
private String urlCDI;
//getters and setters
}
我的SpringApplication.run
班级有这样的照片:
@SpringBootApplication
@EnableConfigurationProperties
@ComponentScan({ "otherPackages", "packageWhereCollectorPropertiesIs" })
当我有我application.properties
的这条线
collector.urlCDI=https://www.cetip.com.br/Home
它的工作原理与其他 spring bean 中的预期相同:
//@Component class variables:
@Autowired
private CollectorProperties props;
//inside some method
URL url = new URL(props.getUrlCDI());
但是当我删除它或更改属性键时,我会得到很多 NPE 而不是验证错误。我做错了什么?不hibernate-validator
包含javax.validation.constraints.NotNull
接口的实现?
解决方案
将“@Validated”注释添加到您的属性类
推荐阅读
- python - 如何在网站上为多个用户运行 Gmail API?
- php - 将 $pdo 从另一个文件传递到 Util.php
- javascript - react.js 最佳方式:在状态更改时显示或隐藏 html 元素
- java - 如何使用 logback 登录到 Linux 上的文件
- node.js - 如何在 Node 中解析多部分/与 GZIP 压缩相关?
- r - 提高可见性/增加表格中一列的字体大小:使用 kableextra 生成的 pdf 输出
- ftp - 网站只有在我登录 FTP 时才可见(?)为什么?
- linux - 使用 pinctrl-single 将板静态引脚映射到设备树中的自身不适用于 Beaglebone
- python - 我正在尝试登录 LinkedIn 以执行网页抓取,但在尝试登录时收到“没有此类元素:无法找到元素”
- postgresql - 如何找到一个月内购买超过 10 次的客户 postgresql?