You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala新手使用Akka调用API时遇到编译错误求助

Hey there! Let's work through these issues step by step—since you're new to Scala and Akka, it's totally normal to hit these bumps right out the gate.

First, let's tackle the "not found" errors

The ActorSystem and ActorMaterializer not being found almost always boils down to missing dependencies in your build file. Akka is split into separate modules, so you need to include the right ones to access these classes. If you're using SBT (the most common build tool for Scala), add these lines to your build.sbt:

// Use compatible versions—Akka HTTP 10.5.x pairs with Akka 2.8.x
val AkkaVersion = "2.8.5"
val AkkaHttpVersion = "10.5.3"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % AkkaVersion,       // For ActorSystem
  "com.typesafe.akka" %% "akka-stream" % AkkaVersion,     // For streaming utilities
  "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion    // For the HTTP client
)

Make sure you reload your SBT project after adding these so the dependencies get downloaded.

Next, the deprecated ActorMaterializer

You're right—ActorMaterializer() was deprecated in Akka 2.6+. The modern replacement is SystemMaterializer, which ties the materializer to your ActorSystem properly. Replace your materializer line with this:

import akka.stream.SystemMaterializer
implicit val materializer = SystemMaterializer(system).materializer

This gives you a properly configured materializer that works with the latest Akka versions.

The ExecutionContext error

This is actually a side effect of the first two errors. Since the compiler couldn't find ActorSystem, it couldn't resolve system.dispatcher either. Once you fix the dependencies and materializer, this error will disappear on its own—your line implicit val executionContext = system.dispatcher is totally correct.

Here's your fixed, updated code

I also added a system.terminate() call in both success and failure cases—this is important because Akka's ActorSystem will keep your program running indefinitely if you don't shut it down explicitly:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.SystemMaterializer
import scala.concurrent.Future
import scala.util.{ Failure, Success }

object Client {
  def main(args: Array[String]): Unit = {
    implicit val system = ActorSystem()
    implicit val materializer = SystemMaterializer(system).materializer
    implicit val executionContext = system.dispatcher

    val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "https://akka.io"))

    responseFuture
      .onComplete {
        case Success(res) => 
          println(s"Success! Response status: ${res.status}")
          system.terminate()
        case Failure(ex) => 
          println(s"Oops, request failed: ${ex.getMessage}")
          system.terminate()
      }
  }
}

Quick recap of what went wrong

  1. Missing dependencies meant the compiler couldn't find Akka's core classes like ActorSystem.
  2. ActorMaterializer is indeed deprecated—switching to SystemMaterializer fixes that.
  3. The ExecutionContext error was just a chain reaction from the first issue, which resolves once the ActorSystem is properly available.

Give this a try, and you should be able to run the HTTP client without issues!

内容的提问来源于stack exchange,提问作者abhishek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:22:50