【问题标题】:Spring Boot YAML: set null property valueSpring Boot YAML:设置空属性值
【发布时间】:2021-03-06 12:47:22
【问题描述】:

我无法将 null 设置为属性值。

这是在 YAML 文件中定义值的方式:

my-property: null

这就是我在代码中注入它的方式:

@Value("${my-property}")
private String myProperty;

出于某种原因,Spring 不断注入一个空字符串 ("") 而不是 null。我错过了什么还是这是 Spring 中的错误?

【问题讨论】:

  • 这仅仅是因为 YAML 文件中的 'null' 被认为是 'null' 字符串,而不是 null 值。要获得您想要的内容,只需完全删除值,然后在 EL 中使用 null 作为默认值:@Value("${my-property:null}")

标签: java spring spring-boot yaml


【解决方案1】:

尝试使用 $ 代替 #。 或者试试这个@Value("${<my-property>}")

【讨论】:

    【解决方案2】:

    你不能,但这不是因为 YAML。 YAML 支持 null 按:

    Empty field in yaml

    这是因为 Spring 处理器类将“null”值转换为空字符串,请参见此处:

    https://github.com/spring-projects/spring-framework/issues/19986

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2017-10-26
      相关资源
      最近更新 更多