libraryDependencies += "net.liftweb" %% "lift-json" % "3.4.3" LiftJsonSupport.serializer = LiftJson scala import net.liftweb.json.JsonDSL._ import net.liftweb.json._ get("/user/:id") { try { val user = getUserFromDB(params("id")) val json = ("id" -> user.id) ~ ("name" -> user.name) ~ ("email" -> user.email) Ok(json) } catch { case e: Exception => } } scala case class ValidationError(message: String) post("/user") { try { val json = parse(request.body) val id = (json \ "id").extract[Int] val name = (json \ "name").extract[String] val email = (json \ "email").extract[String] BadRequest(compactRender(Extraction.decompose(error))) } else { saveUserToDB(id, name, email) } } catch { case e: Exception => } }


上一篇:
下一篇:
切换中文