scala - Op-Rabbit 在 Akka Http 中使用 Spray-Json

标签 scala rabbitmq akka akka-stream akka-http

我正在尝试使用该库 Op-Rabbit在 Akka-Http 项目中使用 RabbitMQ 队列。

我想使用 Spray-Json 进行编码/取消编码。

import com.spingo.op_rabbit.SprayJsonSupport._
import com.spingo.op_rabbit.stream.RabbitSource
import com.spingo.op_rabbit.{Directives, RabbitControl}

object Boot extends App with Config with BootedCore with ApiService  {
  this: ApiService with Core =>

 implicit val materializer = ActorMaterializer()


 Http().bindAndHandle(routes, httpInterface, httpPort)
 log.info("Http Server started")

  implicit val rabbitControl = system.actorOf(Props[RabbitControl])

  import Directives._
  RabbitSource(
    rabbitControl,
    channel(qos = 3),
    consume(queue(
      "such-queue",
      durable = true,
      exclusive = false,
      autoDelete = false)),
    body(as[User])). 
    runForeach { user =>
    log.info(user)
  } // after each successful iteration the message is acknowledged.
}

在单独的文件中:

case class User(id: Long,name: String)

object JsonFormat extends DefaultJsonProtocol {
  implicit val format = jsonFormat2(User)
}

我收到的错误是:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[*.*.models.User]
[error]     body(as[User])). // marshalling is automatically hooked up using implicits
[error]            ^
[error]could not find implicit value for parameter um: com.spingo.op_rabbit.RabbitUnmarshaller[*.*.models.User]
[error]       body(as[User])
[error]              ^
[error] two errors found

我不确定如何让 op-rabbit Spray-json 支持正常工作。

感谢您的帮助。

最佳答案

尝试为您的 User 类提供隐式编码器,就像它们为 Int 所做的那样(在 RabbitTestHelpers.scala 中):

implicit val simpleIntMarshaller = new RabbitMarshaller[Int] with RabbitUnmarshaller[Int] {
    val contentType = "text/plain"
    val contentEncoding = Some("UTF-8")

    def marshall(value: Int) =
      value.toString.getBytes

    def unmarshall(value: Array[Byte], contentType: Option[String], charset: Option[String]) = {
      new String(value).toInt
    }
  }

关于scala - Op-Rabbit 在 Akka Http 中使用 Spray-Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35504827/

相关文章:

c# - 如何让 RabbitMQ 从队列中一个一个读取?

java - 使用哪些 Akka Java 类来实现 Future 回调?

scala - 使用 Akka Streams 读取 CSV 文件

scala - 在 sbt 项目中使用 Maven Central 的处理库时出现奇怪的错误

scala - Quill onconflict更新多个值

scala - 做以_结尾的方法!在Scala中有特殊含义吗?

java - 将参数注入(inject) Akka Props 和 Creator 实例

java - 如何使用 Scala 进行 instanceof 检查(测试)

java - 如何将错误消息移动到rabbitmq死信队列

rabbitmq - 如何启动 RabbitMQ 节点?