我想阻止應用程序屬性的 NotNull 值。
在我的 application.yml
spring:
security:
oauth2:
resourceserver:
my-property: classpath:a/b.json
我的屬性類:
@Data
@Configuration
@ConfigurationProperties("spring.security.oauth2.resourceserver")
public class ABCProperties {
@NotNull
private URI myProperty
當應用程序屬性的值為空時,我沒有約束違規異常。
如何防止應用程序屬性的值為空?
您需要添加@Validate
到您ABCProperties
的如下:
@Data
@Validated
@Configuration
@ConfigurationProperties("spring.security.oauth2.resourceserver")
public class ABCProperties {
@NotNull
private URI myProperty;
}
作為旁注,從 Spring Boot 2.2 開始,Spring@ConfigurationProperties
通過類路徑掃描查找和註冊類。因此,你不需要註釋這種班,@Component
或者@Configuration
甚至使用@EnableConfigurationProperties
。如果您使用的是高於 2.2 的 Spring Boot 版本,則可以@Configuration
從ABCProperties
類中刪除。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句