[英]How to unmarshall `text/plain` as JSON in Akka HTTP
I'm working with a legacy HTTP API (that I can't change) that responds with JSON in the body, but gives a Content-Type: text/plain; charset=utf-8
我正在使用旧的 HTTP API(我无法更改),它在正文中使用 JSON 进行响应,但提供了
Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8
header. Content-Type: text/plain; charset=utf-8
标头。
I am attempting to unmarshall that HTTP body as JSON, but I get the following exception: akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type, supported: application/json
我试图将该 HTTP 正文解组为 JSON,但出现以下异常:
akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type, supported: application/json
My code looks like this:我的代码如下所示:
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.unmarshalling._
case class ResponseBody(status: String, error_msg: String)
object ResponseBodyJsonProtocol extends DefaultJsonProtocol {
implicit val responseBodyFormat = jsonFormat2(ResponseBody)
}
def parse(entity: HttpEntity): Future[ResponseBody] = {
implicit val materializer: Materializer = ActorMaterializer()
import ResponseBodyJsonProtocol._
Unmarshal[HttpEntity](entity).to[ResponseBody]
}
A sample HTTP response looks like this:示例 HTTP 响应如下所示:
HTTP/1.1 200 OK
Cache-Control: private
Content-Encoding: gzip
Content-Length: 161
Content-Type: text/plain; charset=utf-8
Date: Wed, 16 Dec 2015 18:15:14 GMT
Server: Microsoft-IIS/7.5
Vary: Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
{"status":"1","error_msg":"Missing parameter"}
What can I do to ignore the Content-Type
in the HTTP response and parse as JSON?如何忽略 HTTP 响应中的
Content-Type
并解析为 JSON?
One workaround I found was to just manually set the Content-Type
on the HttpEntity
before unmarshalling it:我发现的一种解决方法是在解
HttpEntity
之前手动设置HttpEntity
上的Content-Type
:
def parse(entity: HttpEntity): Future[ResponseBody] = {
implicit val materializer: Materializer = ActorMaterializer()
import ResponseBodyJsonProtocol._
Unmarshal[HttpEntity](entity.withContentType(ContentTypes.`application/json`)).to[ResponseBody]
}
Seems to work OK, but I'm open to other ideas...似乎工作正常,但我对其他想法持开放态度......
I'd use map...
directive.我会使用
map...
指令。 It looks short and elegant.它看起来又短又优雅。
val routes = (decodeRequest & encodeResponse) {
mapResponseEntity(_.withContentType(ContentTypes.`application/json`)) {
nakedRoutes ~ authenticatedRoutes
}
}
An alternative solution: create a custom unmarshaller that accepts any content type (or any list of content types):另一种解决方案:创建一个接受任何内容类型(或任何内容类型列表)的自定义解组器:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.sprayJsValueByteStringUnmarshaller
import akka.http.scaladsl.model.ContentTypeRange
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import spray.json.{RootJsonReader, jsonReader}
// Add this to scope, INSTEAD OF importing SprayJsonSupport._
// It returns an Unmarshaller identical to SprayJsonSupport.sprayJsonUnmarshaller, but with a custom validation on content type
implicit def lenientJsonUnmarshaller[T](implicit reader: RootJsonReader[T]): FromEntityUnmarshaller[T] =
Unmarshaller.byteStringUnmarshaller
.forContentTypes(ContentTypeRange.*) // or any range you'd like
.andThen(sprayJsValueByteStringUnmarshaller)
.map(jsonReader[T].read)
Then - with that in scope, continue as you did:然后 - 在范围内,继续像你一样:
def parse(entity: HttpEntity): Future[ResponseBody] = {
implicit val materializer: Materializer = ActorMaterializer()
import ResponseBodyJsonProtocol._
Unmarshal[HttpEntity](entity).to[ResponseBody] // this implicitly uses the custom unmarshaller
}
The benefit - it's easy to reuse this implicit unmarshaller - write it once and just import it wherever you need, instead of having to set the content-type on the entity.好处 - 很容易重用这个隐式解组器 - 编写一次,只需在需要的地方导入它,而不必在实体上设置内容类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.