@NotNull 約束不適用於應用程序屬性值 spring boot

血紅素

我想阻止應用程序屬性的 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 版本,則可以@ConfigurationABCProperties類中刪除

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章