NodeSeqMarshaller with ContentType charset 无法解析



试图让一个 spray 端点协商邮递员对内容的请求。我的 XML 封送器似乎让我失望,即,它永远不会根据 Accept 标头和字符集解析正确的封送器选项。

我有以下几点:

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!")
        }
      }
    }
  }

不幸的是,我似乎从来没有正确谈判过,即,我的编组器分辨率会将炸弹潜入"任何"块并以 406 响应

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

text/xml; charset=UTF-8"

我正在使用邮递员,我的请求标头为:

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

这可能是一件小而愚蠢的事情 - 希望有人可以帮助引导我朝着正确的方向前进。

谢谢

看看案例类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`))) => ...

最新更新