Created
September 5, 2018 03:55
-
-
Save sebastianharko/6dc679176beaa756d945f7c1cfa300be to your computer and use it in GitHub Desktop.
use different marshaller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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