具有 ContentType 字符集的 NodeSeqMarshaller 无法解析

NodeSeqMarshaller with ContentType charset not resolving

正在尝试获取喷雾端点以协商内容的邮递员请求。我的 XML 编组器似乎让我失望,即,它永远不会根据 Accept header 和字符集解析正确的编组器选项。

我有以下内容:

object ResponseVO {  
   val NodeSeqMarshaller = Marshaller.delegate[ResponseVO, NodeSeq](ContentType(`text/xml`, `UTF-8`)) { respVO => <ussd><type>{ respVO.respType }</type><message>{ respVO.message }</message></ussd> }

   val supportedContentTypes = List[ContentType](ContentType(`text/xml`, `UTF-8`))
   implicit val marshaller = Marshaller[ResponseVO] { (respVO, ctx) =>
   ctx.tryAccept(supportedContentTypes) match {
    case Some(ContentType(`text/xml`, `UTF-8`)) => NodeSeqMarshaller(respVO, ctx)
    case whatever                               => println(whatever); ctx.rejectMarshalling(supportedContentTypes);
  }
}
}

以及以下路线:

trait USSDRoute { 
  this: SimpleRoutingApp with BootStrappedActorSystem  =>
  val ussdRoute = path("ussd") {
                  parameters('msisdn.as[Long], 'session.as[String], 'type.as[Int], 'msg.as[String], 'network.as[Int]) { (msisdn, session, reqType, msg, network) =>
        complete {
          val reqVO = RequestVO(msisdn, session, reqType, msg, network)
          println(s"received $reqVO")
          ResponseVO(1, "Spirit midget medium escapes from prison, headlines read: Small medium at large!")
        }
      }
    }
  }

不幸的是,我似乎从来没有正确协商过,即我的编组器决议会将炸弹投入 "whatever" 块并以 406

响应
"Resource representation is only available with these Content-Types:

text/xml;字符集=UTF-8

我正在使用 Postman,我的请求 header 已读:

GET /ussd?msisdn=0794138690&type=1&network=4&msg=hello&session=99 HTTP/1.1
Host: localhost:9999
Connection: keep-alive
Accept: text/xml; charset=UTF-8
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/40.0.2214.91 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,nl;q=0.6

这可能是一些小而愚蠢的事情 - 希望有人能帮助我指引正确的方向。

谢谢

看看案例class ContentType是如何定义的:

case class ContentType(mediaType: MediaType, definedCharset: Option[HttpCharset])

请注意 definedCharset 被定义为 Option[HttpCharset],因此您的模式匹配通过

case Some(ContentType(`text/xml`, `UTF-8`)) => ...

永远不可能成功。所以你需要使用一个Some来成功执行模式匹配:

case Some(ContentType(`text/xml`, Some(`UTF-8`))) => ...