【问题标题】:How to parse Integer value as keys in YAML File如何将整数值解析为 YAML 文件中的键
【发布时间】:2017-11-17 12:38:22
【问题描述】:

我正在运行一个 spring boot 项目,我需要动态解析错误代码,但我意识到如果密钥是整数类型,它会返回 null。有没有办法解析 YAML 文件中的整数键

解析这个属性不起作用

errorcode:
     00001: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.

Howver Parsing This property work

errorcode:
  ONE: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.

我正在尝试使用

读取此属性
import org.springframework.core.env.Environment;

@Autowired
private Environment environment;

error= environment.getProperty("errorcode.ONE") Works
error= environment.getProperty("errorcode.00001") --Doesn't works

所以简而言之,如果一个整数被用作键,YAML 文件将返回值为 null。一种解决方法是如下所示以这种方式使用密钥,但有更好的想法吗?

error
  code-001:

【问题讨论】:

  • 我没有足够的信息来查看使用了哪个 YAML 处理器。假设 SnakeYAML,您应该注意它将00001 加载为简单的1,因此您可以尝试"errorcode.1"。您也可以尝试调试并输出完整的数据,而不是假设它被转换为 null。

标签: spring-boot yaml


【解决方案1】:

可以通过在 yaml 中将该数字指定为字符串来解决。喜欢,

错误代码:

'00001': An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.

这对我有用。

【讨论】:

    【解决方案2】:

    从 yaml 中读取所有字符串并将整数值替换为非整数值。将具有非整数值的类反序列化为变量。

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2019-10-17
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多