我正在尝试在spray-json
服务器中使用akka-http
,但出现此错误:
Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'spray'
实际上,我的配置中没有spray
设置。我浏览了一些文档,但是仍然找不到如何以及为什么在配置中设置spray json
。可能是我做错了。我完全迷失了自己,陷入困境,请帮忙。
这是我的 buid.sbt :
enablePlugins(JavaServerAppPackaging)
name := "api-server"
version := "0.1"
scalaVersion := "2.12.8"
val akkaVersion = "2.5.21"
val akkaHttpVersion = "10.1.7"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-core" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"ch.qos.logback" % "logback-classic" % "1.2.3"
)
// Assembly settings
mainClass in assembly := Some("company.Main")
assemblyJarName in assembly := "company.jar"
这是我的 plugins.sbt
resolvers += Classpaths.typesafeReleases
addSbtPlugin("com.eed3si9n" %% "sbt-assembly" % "0.14.9")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.19")
谢谢。
/////////////////////////
完整堆栈跟踪:
Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'spray'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:156)
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:149)
at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:176)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:188)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:193)
at com.typesafe.config.impl.SimpleConfig.getString(SimpleConfig.java:250)
at com.epoqee.gotickets.RequestTimeout.requestTimeout(Main.scala:39)
at com.epoqee.gotickets.RequestTimeout.requestTimeout$(Main.scala:38)
at com.epoqee.gotickets.Main$.requestTimeout(Main.scala:13)
at com.epoqee.gotickets.Main$.delayedEndpoint$com$epoqee$gotickets$Main$1(Main.scala:21)
at com.epoqee.gotickets.Main$delayedInit$body.apply(Main.scala:13)
at scala.Function0.apply$mcV$sp(Function0.scala:39)
at scala.Function0.apply$mcV$sp$(Function0.scala:39)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
at scala.App.$anonfun$main$1$adapted(App.scala:80)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.App.main(App.scala:80)
at scala.App.main$(App.scala:78)
at company.Main$.main(Main.scala:13)
at company.Main.main(Main.scala)
此外,这就是我将spray json
集成到项目中的方式:
package company
import spray.json._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
trait EventMarshalling extends SprayJsonSupport with DefaultJsonProtocol {
implicit val eventFormat = jsonFormat1(Event)
...
}
//////////////////////////////// Main.scala
package com.epoqee.gotickets
import scala.concurrent.Future
import akka.actor.ActorSystem
import akka.event.Logging
import akka.util.Timeout
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.stream.ActorMaterializer
import com.typesafe.config.{Config, ConfigFactory}
object Main extends App with RequestTimeout {
val config = ConfigFactory.load()
val host = config.getString("http.host")
val port = config.getInt("http.port")
implicit val system = ActorSystem()
implicit val ec = system.dispatcher
val api = new RestApi(system, requestTimeout(config)).routes
implicit val materializer = ActorMaterializer()
val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port)
val log = Logging(system.eventStream, "go-ticks")
bindingFuture.map { serverBinding =>
log.info(s"RestApi bound to ${serverBinding.localAddress} ")
}.onFailure {
case ex: Exception =>
log.error(ex, "Failed to bind to {}:{}!", host, port)
system.terminate()
}
}
trait RequestTimeout {
import scala.concurrent.duration._
def requestTimeout(config: Config): Timeout = {
val t = config.getString("spray.can.server.request-timeout")
val d = Duration(t)
FiniteDuration(d.length, d.unit)
}
}
答案 0 :(得分:0)
仅供参考
我最终使用设置akka.http.server.request-timeout
来配置我的requestTimeout
trait RequestTimeout {
import scala.concurrent.duration._
def requestTimeout(config: Config): Timeout = {
val t = config.getString("akka.http.server.request-timeout") // <<==
val d = Duration(t)
FiniteDuration(d.length, d.unit)
}
}