无法创建配置对象

时间:2019-03-22 07:51:58

标签: scala playframework-2.6

我想用代码创建以下配置。

mail{
  signupUrl = "/test/ws/users/signup/"
  signupFrom="Test<mailrobot@test.com>"
  signupReply="Test<noreply@test.comm>"

}

我尝试了以下两种方法,但遇到错误

尝试1. API(https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.Configuration $)具有from方法,该方法采用Map

val mailConfig = Map("signupUrl" -> "/test/ws/users/signup/",
        "signupFrom"->"Test<mailrobot@test.com>",
        "signupReply"->"Test<noreply@test.comm>")
      val newConfig = Configuration.from(Map("mail"->mailConfig))
      val newConfiguration = Configuration(newConfig)

错误

Error:(575, 30) overloaded method value apply with alternatives:
  (underlying: com.typesafe.config.Config)play.api.Configuration <and>
  (data: (String, Any)*)play.api.Configuration
 cannot be applied to (play.api.Configuration)
      val newConfiguration = Configuration(newConfig)

尝试2-API(https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.Configuration $)具有apply方法,该方法采用(String,any*)

 val mailConfig = ("signupUrl" -> "/test/ws/users/signup/",
        "signupFrom"->"Test<mailrobot@test.com>",
        "signupReply"->"Test<noreply@test.comm>")
      val newConfig = ("mail"->mailConfig)
      val newConfiguration = Configuration(newConfig)

错误

bug in method caller: not valid to create ConfigValue from: ((signupUrl,/test/ws/users/signup/),(signupFrom,Test<mailrobot@test.com>),(signupReply,Test<noreply@test.comm>))

1 个答案:

答案 0 :(得分:0)

如果您仅删除最后一行,则您的第一个版本有效,例如:

val mailConfig = Map("signupUrl" -> "/test/ws/users/signup/",
        "signupFrom"->"Test<mailrobot@test.com>",
        "signupReply"->"Test<noreply@test.comm>")
      val newConfig = Configuration.from(Map("mail"->mailConfig))

val newConfig: Configuration = Configuration.from(Map("mail"->mailConfig))

已经返回配置。

如果要从key -> value对创建配置,则需要:

  • 将对放入Seq
  • Seq转换为varargs:_*)。

这里是示例:

val mailConfig = Seq("signupUrl" -> "/test/ws/users/signup/",
      "signupFrom"->"Test<mailrobot@test.com>",
      "signupReply"->"Test<noreply@test.comm>")
    val newConfiguration = Configuration(mailConfig: _*)