如何知道 unmarshell 是否成功
How to know, if unmarshell was successful or not
我有一条路线,它将把传入的实体解组到一个案例中 class。
final case class ProducerMessage(topic: String, event: String, data: spray.json.JsObject)
object ProducerServer {
private val route: Route =
path("producer") {
post {
entity(as[ProducerMessage]) { msg =>
//complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
}
def create(): Future[ServerBinding] {
Http().bindAndHandle(route, getServerIp, getServerPort)
}
}
我怎么知道解组过程是否成功?
当接收到的数据不是有效的 JSON 格式时,会发生什么?
当你有 entity(as[T])
时,as[T]
用于召唤 FromRequestUnmarshaller[T]
的实例 - 然后根据 unmarshaller 返回的结果,entity
将继续传递T
进入关闭状态,否则 Directive
.
将失败
如果需要对rejection的信息进行处理,可以在apply
之前调用recover
等方法。
例如:
entity(as[ProducerMessage])
.map(Right(_): Either[Seq[Rejection], ProducerMessage])
.recover { rejections =>
provide(Left(rejections): Either[Seq[Rejection], ProducerMessage]))
} { value: Either[Seq[Rejection], ProducerMessage] =>
...
}
应该让您查看输入是否被拒绝并手动 recover/handle 它。
我有一条路线,它将把传入的实体解组到一个案例中 class。
final case class ProducerMessage(topic: String, event: String, data: spray.json.JsObject)
object ProducerServer {
private val route: Route =
path("producer") {
post {
entity(as[ProducerMessage]) { msg =>
//complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
}
def create(): Future[ServerBinding] {
Http().bindAndHandle(route, getServerIp, getServerPort)
}
}
我怎么知道解组过程是否成功? 当接收到的数据不是有效的 JSON 格式时,会发生什么?
当你有 entity(as[T])
时,as[T]
用于召唤 FromRequestUnmarshaller[T]
的实例 - 然后根据 unmarshaller 返回的结果,entity
将继续传递T
进入关闭状态,否则 Directive
.
如果需要对rejection的信息进行处理,可以在apply
之前调用recover
等方法。
例如:
entity(as[ProducerMessage])
.map(Right(_): Either[Seq[Rejection], ProducerMessage])
.recover { rejections =>
provide(Left(rejections): Either[Seq[Rejection], ProducerMessage]))
} { value: Either[Seq[Rejection], ProducerMessage] =>
...
}
应该让您查看输入是否被拒绝并手动 recover/handle 它。