Raaj Ashtaputre
Raaj Ashtaputre

Reputation: 51

Can't resolve symbol respondWithMediaType - Scala/Spray/Json

For some reason, my code wont resolve this symbol "respondWithMediaType" despite having all necessary imports. I'm fairly new to both spray and scala - so perhaps missing something obvious?

 import spray.http.MediaTypes._
 import spray.json._
 import DefaultJsonProtocol._
 .....
 trait Service extends CassandraSpec with UsersService {
   implicit val system: ActorSystem

   implicit def executor: ExecutionContextExecutor

   implicit val materializer: Materializer
   implicit val timeout: Timeout
   implicit val jsonFormatUsers = jsonFormat5(Users)
   implicit val jsonFormatAllUsers = List(jsonFormat5(Users))
   .....

 pathPrefix("users") {
    (get & path(Segment)) { email =>
        respondWithMediaType(MediaTypes.`application/json`) {
        service.getByUsersEmail(email)
      }
    }
    get {
      // GET /users
      path(Rest) {
        respondWithMediaType(`application/json`) {
          service.getAllUsers()
        }
      }
    } ~
    post {
        entity(as[Users]) { users: Users =>
          respondWithMediaType(`application/json`) {
            service.saveOrUpdate(users)
          }
        }
      }
  }

}

Upvotes: 0

Views: 660

Answers (1)

grotrianster
grotrianster

Reputation: 2468

As I understand you want to send back json then you do not need to set content type, spray-json will do it for you, replace respondWithMediaType with complete method like this:

post {
        entity(as[Users]) { users: Users =>
          complete {
            service.saveOrUpdate(users)
          }
        }
      }

and make sure you got all spray-json imports, more info here

Upvotes: 0

Related Questions