【问题标题】:how to force as mandatory a String when binding an application.properties file?绑定application.properties文件时如何强制强制字符串?
【发布时间】:2021-10-13 10:39:54
【问题描述】:

我有类似的东西..

在文件 application.properties 中

  input.param.resolution=high
  input.param.size=1024

在配置类中

  @Configuration
  public class FirstConfiguration {

  @Bean
    @ConfigurationProperties("input.param")
        Data buildDataBean() {
        return new Data();
     }
  }

我的宝珠

  public class Data {

      String resolution;
      String size;

     // getters and setters 
  }

当属性文件中未提供我的字符串时,我想取消授权绑定..(并使 Spring 启动异常)

    input.param.resolution=<nothing-here>
    input.param.size=<nothing-here>

我该怎么做?

【问题讨论】:

    标签: java spring-boot application.properties


    【解决方案1】:

    尝试如下验证:

    @Validated
    public class Data {
        @NotNull
        String resolution;
    
        @NotNull
        String size;
    
       // getters and setters 
    }
    

    如果您还没有它,您需要将以下依赖项添加到您的项目中:

    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-validation</artifactId> 
    </dependency>
    

    【讨论】:

    • 我试过但没有任何效果:-(
    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 2015-10-21
    • 2012-03-14
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    相关资源
    最近更新 更多