【问题标题】:Trying to get a list with the Typesafe config library尝试使用 Typesafe 配置库获取列表
【发布时间】:2016-11-27 09:01:38
【问题描述】:

我尝试根据此示例从配置中获取列表:How to get a list with the Typesafe config library

但是,我得到以下异常:

Exception in thread "main" com.typesafe.config.ConfigException$WrongType: application.properties @ file:/xxx/application.properties: configYYY has type STRING rather than LIST
    at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:159)
    at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:170)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:184)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:189)
    at com.typesafe.config.impl.SimpleConfig.getList(SimpleConfig.java:252)
    at com.typesafe.config.impl.SimpleConfig.getHomogeneousUnwrappedList(SimpleConfig.java:323)
    at com.typesafe.config.impl.SimpleConfig.getStringList(SimpleConfig.java:381)

如何从 typesafe 获取列表?以下是我的测试代码:

class Test extends FlatSpec {
  "Test" should "be about to get list" in {

    val configFactory = ConfigFactory.load();
    var disabledExtension = configFactory.getStringList("disabledExtension");
    assert(2==disabledExtension.size());
    assert(disabledExtension.get(0).equals("SH"));
    assert(disabledExtension.get(1).equals("ST"));
  }
}

在我的 application.properties 下面:

disabledExtension = ["SH", "ST"]

【问题讨论】:

  • 什么是配置,你正在尝试什么代码?
  • 该错误表示您在此键处没有列表,而是一个字符串。例如。如果您有 a, b 之类的内容,则应改为 ["a", "b"]
  • 是的,我已经在使用 ["a", "b"] 了,但是它仍然报这个错误,让我先尝试一些更简单的方法
  • 您找到解决方案了吗?
  • 不,我自己解析它,disabledExtension = SH|ST

标签: scala typesafe


【解决方案1】:

application.properties 必须是 application.conf

“*.properties”文件中的任何属性都将被读取为字符串。

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 2021-05-14
    • 1970-01-01
    • 2018-06-13
    • 2021-08-22
    • 2017-03-20
    • 1970-01-01
    • 2016-04-24
    • 2013-09-11
    相关资源
    最近更新 更多