使用Akka http中的封送器发送包含Json内容的http响应
我想发送一个Http错误响应,在正文中包含一条JSON格式的消息。我在使用电脑时遇到问题 我在中看到了一个实现,但我尝试了类似的方法,它抛出了一个编译错误使用Akka http中的封送器发送包含Json内容的http响应,json,scala,marshalling,akka-http,argonaut,Json,Scala,Marshalling,Akka Http,Argonaut,我想发送一个Http错误响应,在正文中包含一条JSON格式的消息。我在使用电脑时遇到问题 我在中看到了一个实现,但我尝试了类似的方法,它抛出了一个编译错误 import argonaut._, Argonaut._ import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.model.HttpResponse impor
import argonaut._, Argonaut._
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.marshalling.{ Marshaller, ToResponseMarshaller }
trait Sample extends Marshallers with Directives {
def login(user: Login): CRIX[HttpResponse] = {
for {
verification ← verify(user)
resp = if (verification) {
HttpResponse(NoContent, headers = Seq(
.........
))
}//below is my http Error response
else Marshal(401 → "It is an Unauthorized Request".asJson).to[HttpResponse]
} yield resp
}
}
它给出了以下编译错误:
Sample.scala:164: type mismatch;
[error] found : Object
[error] required: akka.http.scaladsl.model.HttpResponse
[error] } yield resp
[error] ^
[error] one error found
[error] (http/compile:compileIncremental) Compilation failed
我刚刚开始Akka Http,所以请原谅我,如果它很简单的话
TL;DR:我想(举例)学习如何在Akka Http中使用响应器 方法
对[HttpResponse]
的负面条件承担未来的[HttpResponse]
。同时,正条件返回HttpResponse
尝试类似的方法(我假设verify
承担Future[T]
):
用于{
验证
for {
verification <- verify(user)
resp <- if (verification)
Future.successful(HttpResponse(NoContent, headers = Seq(.........)) )
else
Marshal(401 → "It is an Unauthorized Request".asJson).to[HttpResponse]
} yield resp