如何使用Scala Play2.6和适当的json响应标头从数据库中选择多列

时间:2018-09-13 17:10:56

标签: scala playframework slick

我有一个表用户详细信息表,它包含在下面的列中

用户名,名称,电子邮件,手机号,名字,姓氏,地址

我想选择用户ID为1的这4列(User_ID,Name,email,Mobile_no)数据,并且我希望以正确的JSON格式进行响应

如何使用Slick在Scala Play 2.6中做到这一点 到目前为止,我已经做到了

import play.api.mvc._
//import slick.driver.H2Driver.api._
import slick.jdbc.H2Profile.api._
import Services.HorseImageServices
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import com.google.gson.Gson

case class userss(User_ID:String,Name:String,email:String,Mobile_no:String)
class Testing @Inject()(cc: ControllerComponents) extends AbstractController(cc){


  def getPasswqord(username :String) = Action{
    import play.api.libs.json.{JsPath, JsValue, Json, Writes}
    val dbConfig = Database.forURL("jdbc:mysql://localhost:3306/equineapp?user=root&password=123456", driver = "com.mysql.jdbc.Driver")
    val a1=(sql"""select User_ID,Name,email,Mobile_no from equineapp.user_details_table where email=$username or Mobile_no=$username """.as[(String,String,String,String)]     )
    val res = Await.result(dbConfig.run(a1), 1000 seconds)

    val gson: Gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create

    //   val json = Json.toJson(res)
    //select User_ID,Name,email,Mobile_no from equineapp.user_details_table where email='' or Mobile_no=''
   // val writes : Writes[(String, String,String,String)] = (JsPath \ "password").write[String] and (JsPath \ "password").write[String]
    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    implicit val locationWrites: Writes[(userss)] = (
      (JsPath \ "userid").write[String] and
        (JsPath \ "Name").write[String] and
        (JsPath \ "Email").write[String] and
        (JsPath \ "Mobile").write[String]
      )(unlift(userss.unapply))
    def toJson[T](sequence: Seq[T])(implicit writes: Writes[T]): JsValue = {
      Json.toJson(sequence)
    }

    implicit val residentWrites = Json.writes[(String,String,String,String)]
    Ok(toJson(res)(residentWrites))

   // Ok(Json.toJson(res.map(gson.toJson).map(Json.parse)))

  }

}

我从中收到格式

[{“ _ 1”:“ 86”,“ _ 2”:“ Abhinay”,“ _ 3”:“ abhinay4cs@gmail.com”,“ _ 4”:“ 9739239812”}]

预期的输出格式为

[{“ userid”:“ 86”,“ Name”:“ Abhinay”,“ Email”:“ abhinay4cs@gmail.com”,“ Mobile”:“ 9739239812”}]

2 个答案:

答案 0 :(得分:0)

您使用了错误的自定义编写器。

您需要使用 locationWrites 而不是 residentWrites

您的解决方案: Ok(toJson(res)(locationWrites))

答案 1 :(得分:0)

假设您有案例类:

case class User(userId: String, name: String, email: String, mobilePhone: String)

要能够将sql"""select User_ID,Name,email,Mobile_no from equineapp.user_details_table where email=$username or Mobile_no=$username """的结果转换为User,您需要定义隐式GetResult

implicit val getUserResult = GetResult(r => User(r.nextString, r.nextString, r.nextString, r.nextString))

将此getUserResult放入调用数据库的范围并将查询结果转换为案例类sql"""your sql query""".as[User]

要能够将User实例转换为json,请使用自定义Writes[(User)]来定义Companion object

object User {
  implicit val jsonWrites: Writes[(User)] = (
      (JsPath \ "userid").write[String] and
        (JsPath \ "Name").write[String] and
        (JsPath \ "Email").write[String] and
        (JsPath \ "Mobile").write[String]
      )(unlift(User.unapply))
}

现在您可以通过这种方式在控制器中将结果转换为json(由于其在同伴对象中定义,因此将自动查找Json写入内容)

Ok(toJson(res))