Json 响应在 scala slick 中

Json response In scala slick

我正在 Scala 控制器中执行存储过程 class 但无法以正确的 json 格式获得结果

    val dbConfig = Database.forURL("jdbc:mysql://localhost:3306/equineapp?user=root&password=123456", driver = "com.mysql.jdbc.Driver")
    val setup1 =  sql"call HorsePrfile ($HorseId);".as[(Int,String)]
    val res = Await.result(dbConfig.run(setup1), 1000 seconds)
 //   val json = Json.toJson(res)
    Ok(Json.toJson(res.toList))

任何人都可以告诉我如何 return json 从上面的代码响应 header 也

这是我的模特class

case class HorseProfile(HorseID: Int,HorsePicUrl:String)
case class HorseProfileData(HorseID: Int, HorsePicUrl:String)
object HorseForm1 {

  val form = Form(
    mapping(
      "HorseID" -> number,
      "HorsePicUrl" ->nonEmptyText ,


    )(HorseProfileData.apply)(HorseProfileData.unapply)
  )
  implicit val fooWrites: Writes[HorseProfile] = (
    (__ \ 'HorseID).write[Int] and (__ \ 'HorsePicUrl).write[String]
    )(foo => (foo.HorseID, foo.HorsePicUrl))
 // val horsepics = TableQuery[HorseProfilePicDef]

}
//
//
class HorseProfilePicDef(tag: Tag) extends Table[HorseProfile](tag, "horse_profile_image") {

  def HorseID1 = column[Int]("horseid")
  def HorsePicUrl = column[String]("horseimage")
  //def HorsePics = column[Blob]("horseimage")

 // def * = (HorseID, HorsePics)
  override def * =
    (HorseID1, HorsePicUrl) <>(HorseProfile.tupled, HorseProfile.unapply)
}

object HorseProfilePics {
  val dbConfig = Database.forURL("jdbc:mysql://localhost:3306/equineapp?user=root&password=123456", driver = "com.mysql.jdbc.Driver")
  //val dbConfig1 = dbConfig.get[JdbcProfile](Play.current)
  val horsepics = TableQuery[HorseProfilePicDef]
  implicit val HorseProfile: Writes[HorseProfile] = Json.writes[HorseProfile]
  implicit val userJsonFormat = Json.format[HorseProfile]





//  val itemWrites: OWrites[(HorseProfile)] = (
//    (__ \ "flight").write[HorseProfile]
////      (__ \ "scheduledFlight").write[ScheduledFlight] and
////      (__ \ "airline").write[Airline] and
////      (__ \ "airport1").write[Airport] and
////      (__ \ "airport2").write[Airport]
//    ).tupled
  implicit val testWriter: OWrites[HorseProfile] = Json.writes[HorseProfile]
  //val resultWrites: Writes[Seq[( HorseProfile )]] = Writes.seq(HorseProfile)
  implicit def seqWrites[T](implicit fmt: Writes[T]): Writes[Seq[T]] = new Writes[Seq[T]] {
    def writes(ts: Seq[T]) = JsArray(ts.toList.map(t => toJson(t)(fmt)))

  }


//  def HorseImage(HorseId : Int): Future[String] = {
//
//
//    val setup1 = sql"call HorsePrfile ($HorseId);".as[(Int, String)]
//
//
//    //val res = Await.result(dbConfig.run(setup1), 1000 seconds)
//    dbConfig.run(setup1).map(seqWrites =>seqWrites.toString).recover {
//      case ex: Exception => ex.getCause.getMessage
//
//
//    }
//  }



//  def writes(tweet: HorseProfile): JsValue = {
//    //  tweetSeq == Seq[(String, play.api.libs.json.JsString)]
//    val tweetSeq = Seq(
//      "username" -> JsString(tweet.HorsePicUrl)
//
//    )
//    JsObject(tweetSeq)
//  }



//  implicit val locationWrites: Writes[HorseProfile] = (
//    (JsPath \ "HorseID").write[Int] and
//      (JsPath \ "HorsePicUrl").write[String]
//    )(unlift(HorseProfile.))

  val resultWrites: Writes[Seq[(HorseProfile)]] = Writes.seq(testWriter)
//  def HorseImage( HorseId : Int): Future[String]= {
//    val a1=horsepics.filter(i => i.HorseID === HorseId)
//    dbConfig.run(horsepics.filter(_.HorseID === HorseId).result.headOption).map(results =>results.toString)
//
//   // dbConfig.run(horsepics.filter(_.HorseID === HorseId).result.headOption
//
//  }


//  def addnew(HorseId:Int,Horsepicurl:String):Future[String]=
//  {
//
//    val project = HorseProfile(HorseId,Horsepicurl)
//    dbConfig.run(HorseProfilePics+=project).map(res => "User successfully added").recover {
//      case ex: Exception => ex.getCause.getMessage
//    }
//
//    //dbConfig.run(users.insertOrUpdate(project))
//  }



  def gethorseimage(HorseId : Int):Future[Seq[HorseProfile]] = {
   // dbConfig.run(horsepics.filter(i => i.HorseID === HorseId ).result.headOption)
   val set=horsepics.filter(_.HorseID1 === HorseId )

    dbConfig.run(set.result)


  }
//

我已将此处的列声明为字符串,但在我的数据库中,我已将此列声明为 BLOB 并尝试以 Json 格式

插入数据和检索数据

首先,永远不要在实际实施中使用 Await.result。如果您不想使用它的并发能力,那么使用 Slick 有什么意义呢?

其次,您需要映射结果,例如:

dbCall.map{
  returnedData => 
    ??? //The following step 3 and four here
}.recover{
  //In case something went wrong with your DB call.
  case e => InternalServerError("Db failure")
}  

第三,您需要将您的 returnedData 表示为一个案例 class。

第四,您使用 Json 编写器将案例 class 转换为它的 Json 表示:

implicit val someVal: OWrites[SomeCaseClass] = Json.writes[SomeCaseClass]
Json.toJson(someVal)

更新

因此,根据您的评论,我实施了它,以便它使用您的 return 类型并将其转换为 Json。这是:

import play.api.libs.json.{Json, OWrites}

case class HorseProfile(i: Int, value: String)

val dbResult: Option[HorseProfile] = Some(HorseProfile(77,"iVBORw0KGgoAAAANSUhE"))

implicit val horseProfileWrites: OWrites[HorseProfile] = Json.writes[HorseProfile]

Json.toJson(dbResult)

我得到的结果是:

res0: play.api.libs.json.JsValue = {"i":77,"value":"iVBORw0KGgoAAAANSUhE"}