Parcourir la source

Move Json writer out side of the action method

The writer would be instantiated on every request when it's inside the action.
Amir Karimi il y a 9 ans
Parent
commit
8fd3edf369

+ 3 - 2
frameworks/Scala/play2-scala/play2-scala/app/controllers/Application.scala

@@ -14,10 +14,11 @@ class Application extends Controller {
 
   // Easy ones
   case class HelloWorld(message: String)
+  implicit val helloWorldWrites = Json.writes[HelloWorld]
 
   def getJsonMessage = Action {
     val helloWorld = HelloWorld(message = "Hello, World!")
-    Ok(Json.toJson(helloWorld)(Json.writes[HelloWorld])).withHeaders(CONTENT_TYPE -> "application/json")
+    Ok(Json.toJson(helloWorld)).withHeaders(CONTENT_TYPE -> "application/json")
   }
 
   val plaintext = Action {
@@ -25,4 +26,4 @@ class Application extends Controller {
     // BUT the test harness has a WARN state and says we don't need it.
     Ok("Hello, World!").withHeaders(CONTENT_TYPE -> "text/plain")
   }
-}
+}