Skip to content

Instantly share code, notes, and snippets.

@sebastianharko
Created September 5, 2018 03:55
Show Gist options
  • Save sebastianharko/6dc679176beaa756d945f7c1cfa300be to your computer and use it in GitHub Desktop.
Save sebastianharko/6dc679176beaa756d945f7c1cfa300be to your computer and use it in GitHub Desktop.
use different marshaller
bject WebServer extends Json4sSupport {
def main(args: Array[String]) {
implicit val serialization = json4s.jackson.Serialization // or native.Serialization
implicit val formats = DefaultFormats
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val route =
path("hello") {
get {
parameter('raw.as[Boolean]) { raw: Boolean =>
raw match {
case true =>
// json4s is not used here, we import the default predefined marshaller
import akka.http.scaladsl.marshalling.PredefinedToEntityMarshallers._
complete(Stuff("hello").body) // this will return plain text
case false =>
// json4s is user here
complete(Stuff("hello"))
}
}
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment