我用AKKA-http创建我的第一个应用程序。我目前正在使用spray-json将对象写入json。当我创建GET请求时,一切正常,但我尝试了POST请求,并显示以下错误:
Error:(55, 26) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[nl.quintor.model.Employee]
entity(as[Employee]) { request =>
这是我的主要课程:
object Main extends App with Routes {
implicit val system = ActorSystem("system")
implicit val executor: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer()
override val slickboard = createSlickboard
Http().bindAndHandle(routes, httpInterface, httpPort)
private def createSlickboard: ActorRef =
system.actorOf(Slickboard.props(system), applicationName)
}
}
我在以下特征中定义了我的路线:
trait Routes extends CORSSupport with Protocols {
val slickboard: ActorRef
implicit val timeout = Timeout(5 seconds)
val routes =
corsHandler {
pathPrefix("employees") {
pathEnd {
get {
complete {
(slickboard ? GetAllEmployees).mapTo[HttpResponse]
}
}
}
} ~
pathPrefix("employee") {
path(IntNumber) { id =>
get {
complete {
(slickboard ? GetEmployeeById(id)).mapTo[HttpResponse]
}
} ~
post {
decodeRequest {
entity(as[Employee]) { request =>
complete {
request.toString()
}
}
}
}
}
}
}
}
我定义了我的协议就像喷雾推荐:
trait Protocols extends DefaultJsonProtocol {
implicit val employeeFormat = jsonFormat8(Employee.apply)
}
我尝试转换为json的课程如下:
case class Employee(ID: Int, firstname: String, lastname: String, street: String, zipCode: String, city: String, phoneNumber: String, image: String)
我尝试了在堆栈溢出中找到的多个解决方案,但它们都没有实际工作。
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
我怀疑你需要
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._