Created
August 23, 2012 05:27
-
-
Save masahitojp/3432957 to your computer and use it in GitHub Desktop.
scalatra-lift-jsonの練習
This file contains hidden or 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
| organization := "com.github.masahitojp" | |
| name := "scalatra-sbt-prototype" | |
| version := "0.1.0-SNAPSHOT" | |
| scalaVersion := "2.9.2" | |
| seq(webSettings :_*) | |
| classpathTypes ~= (_ + "orbit") | |
| libraryDependencies ++= Seq( | |
| "org.scalatra" % "scalatra" % "2.1.0", | |
| "org.scalatra" % "scalatra-scalatest" % "2.1.0" % "test", | |
| "org.scalatra" % "scalatra-scalate" % "2.1.0", | |
| "org.scalatra" % "scalatra-lift-json" % "2.1.0", | |
| "ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime", | |
| "org.eclipse.jetty" % "jetty-webapp" % "8.1.5.v20120716" % "container", | |
| "org.eclipse.jetty" % "test-jetty-servlet" % "8.1.5.v20120716" % "test", | |
| "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")) | |
| ) |
This file contains hidden or 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
| curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{\"id\":1,\"name\":\"name daze\", \"age\":19}" http://localhost:8080/json |
This file contains hidden or 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
| addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0") | |
| libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % (v+"-0.2.11.1")) |
This file contains hidden or 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
| package com.github.masahitojp.testApp | |
| import org.scalatra._ | |
| import liftjson.LiftJsonSupport | |
| import scalate.ScalateSupport | |
| import net.liftweb.json._ | |
| case class Person(id: Long, name: String, age: Int) | |
| class MyScalatraServlet extends ScalatraServlet with ScalateSupport with LiftJsonSupport { | |
| get("/") { | |
| <html> | |
| <body> | |
| <h1>Hello, world!</h1> | |
| Say <a href="hello-scalate">hello to Scalate</a>. | |
| </body> | |
| </html> | |
| } | |
| get("/json") { | |
| Extraction.decompose( | |
| Person(1, "ぱみゅぱみゅ", 19) | |
| ) | |
| } | |
| post("/json") { | |
| parsedBody match { | |
| case JNothing ⇒ halt(400, "invalid json") | |
| case json: JObject ⇒ { | |
| //(json \ "name").extract[String] | |
| val pamyu: Person = json.extract[Person] | |
| pamyu.name | |
| } | |
| case _ ⇒ halt(400, "unknown json") | |
| } | |
| } | |
| notFound { | |
| // remove content type in case it was set through an action | |
| contentType = null | |
| // Try to render a ScalateTemplate if no route matched | |
| findTemplate(requestPath) map { path => | |
| contentType = "text/html" | |
| layoutTemplate(path) | |
| } orElse serveStaticResource() getOrElse resourceNotFound() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment