I want to send a Http error response with a message in JSON format in the body. I am having trouble using the PredefinedToResponseMarshallers.
我想在正文中发送一条JSON格式的Http错误响应。我在使用预定义的toresponsemarshallers时遇到了麻烦。
I saw an implementation in the Akka docs but I tried a similar thing and it throws a compilation error.
我在Akka文档中看到了一个实现,但是我尝试了一个类似的东西,它抛出了一个编译错误。
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
}
}
It gives this compilation error :
它给出了这个编译错误:
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
I have just started Akka Http so forgive me if it is trivially easy.
我刚开始使用Akka Http,所以请原谅我,如果它很简单。
TL;DR: I want(examples) to learn how to use ToResponseMarshallers in Akka Http.
我想(举例)学习如何在Akka Http中使用ToResponseMarshallers。
1 个解决方案
#1
1
Method to[HttpResponse]
of negative condition takes on Future[HttpResponse]
. At the same time positive condition returns HttpResponse
.
方法[HttpResponse]对不良情况进行预测[HttpResponse]。同时,正值条件返回HttpResponse。
Try something like (I assume verify
takes on 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
#1
1
Method to[HttpResponse]
of negative condition takes on Future[HttpResponse]
. At the same time positive condition returns HttpResponse
.
方法[HttpResponse]对不良情况进行预测[HttpResponse]。同时,正值条件返回HttpResponse。
Try something like (I assume verify
takes on 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