0
votes

I'm creating my first application with AKKA-http. I'm currently using spray-json to write object to json. When I created GET request everything is working fine, but I tried a POST request and the following error shows:

Error:(55, 26) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[nl.quintor.model.Employee]
            entity(as[Employee]) { request =>

This is my main class:

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)
  }
}

I defined my routes in the following trait:

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()
                  }
                }
              }
            }
          }
        }
    } 
}

I defined my protocol just like spray recommended:

trait Protocols extends DefaultJsonProtocol {
  implicit val employeeFormat = jsonFormat8(Employee.apply)
}

The class I'm trying to convert to json is the following:

case class Employee(ID: Int, firstname: String, lastname: String, street:  String, zipCode: String, city: String, phoneNumber: String, image: String)

I tried multiple solutions found in stack overflow, but none of them are actually working.

Could someone please help me with this problem?

1

1 Answers

0
votes

I suspect you need

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._