spray-json 找不到 Class 的 JsonWriter 或 JsonFormat 类型 class

spray-json Cannot find JsonWriter or JsonFormat type class for Class

我仍然遇到同样的错误,我已经定义了编组器(并导入了它);当函数是多态时, case class 条目似乎不在上下文中。这会抛出 Cannot find JsonWriter or JsonFormat type class for Case Class。 spray-json 找不到案例 class 的隐式编组器是否有原因(即使已定义)在上下文中是否是案例 class? Link 到 marshaller

import spray.json._
import queue.MedusaJsonProtocol._

object MysqlDb {
 ...
}

case class UserDbEntry(
  id: Int,
  username: String,
  countryId: Int,
  created: LocalDateTime
)

trait MysqlDb {
  implicit lazy val pool = MysqlDb.pool
}

trait HydraMapperT extends MysqlDb {
  val FetchAllSql: String
  def fetchAll(currentDate: String): Future[List[HydraDbRow]]

  def getJson[T](row: T): String
}

object UserHydraDbMapper extends HydraMapperT {
  override val FetchAllSql = "SELECT * FROM user WHERE created >= ?"

  override def fetchAll(currentDate: String): Future[List[UserDbEntry]] = {
    pool.sendPreparedStatement(FetchAllSql, Array(currentDate)).map { queryResult =>
      queryResult.rows match {
        case Some(rows) =>
          rows.toList map (x => rowToModel(x))
        case None => List()
      }
    }
  }

  override def getJson[UserDbEntry](row: UserDbEntry): String = {
      HydraQueueMessage(
        tableType = HydraTableName.UserTable,
        payload = row.toJson.toString()
      ).toJson.toString()
  }

  private def rowToModel(row: RowData): UserDbEntry = {
    UserDbEntry (
      id        = row("id").asInstanceOf[Int],
      username  = row("username").asInstanceOf[String],
      countryId = row("country_id").asInstanceOf[Int],
      created   = row("created").asInstanceOf[LocalDateTime]
    )
  }
}

payload = row.toJson.toString() 找不到 UserDbEntry 的编组器

您已在本地定义了 UserDbEntry,但该类型没有 JSON 编组器。添加以下内容:

 implicit val userDbEntryFormat = Json.format[UserDbEntry]

鉴于 UserDbEntry 是本地案例 class,我不确定如何调用 row.toJson。那里一定有一个宏,但很明显它不在本地 UserDbEntry.

的范围内

编辑

现在我看到 your Gist,看来您遇到了包依赖性问题。按照设计,它将是圆形的。您在 package com.at.medusa.core.queue 中定义了 JSON 编组器,它导入 UserDbEntry,这取决于 package com.at.medusa.core.queue 进行编组。