gpt4 book ai didi

scala - akka http 使用 Json 支持和 xmlsupport

转载 作者:行者123 更新时间:2023-12-02 20:29:39 26 4
gpt4 key购买 nike

当部门为“hr”时,我想以 xml 格式打印数据,而当部门为“tech”时,我想以 json 格式打印数据。

我们可以使用Spray-json 支持 https://doc.akka.io/docs/akka-http/current/common/json-support.html和 XML 支持 https://doc.akka.io/docs/akka-http/current/common/xml-support.html一起

 private[rest] def route =
(pathPrefix("employee") & get) {
path(Segment) { id =>
parameters('department ? "") { (flag) =>
extractUri { uri =>
complete {
flag match {
case "hr": => {

HttpEntity(MediaTypes.`application/xml`.withCharset(HttpCharsets.`UTF-8`),"hr department")
}
case "tech" =>{
HttpEntity(ContentType(MediaTypes.`application/json`), mapper.writeValueAsString("tech department"))

}

}
}
}
}
}
}

我尝试过的解决方案我通过使用 JsonProtocols 和 ScalaXmlSupport 尝试了以下操作,我得到了预期为 ToResponseMarshallable 的编译错误,但发现了 Department

case class department(name:String)
private[rest] def route =
(pathPrefix("employee") & get) {
path(Segment) { id =>
parameters('department ? "") { (flag) =>
extractUri { uri =>
complete {
flag match {
case "hr": => {

complete(department(name =flag))
}
case "tech" =>{
complete(department(name =flag))

}

}
}
}
}
}
}

最佳答案

我认为您必须克服几个问题才能实现您想要的目标:

  1. 您想要根据请求参数自定义响应类型。这意味着基于标准隐式的编码(marshal)处理对您不起作用,您必须执行一些显式步骤

  2. 您想要将一些业务对象编码到 XML 字符串中。不幸的是,ScalaXmlSupport您引用的不支持此功能,它只能将 XML 树编码到响应中。因此您需要一些可以执行 XML 序列化的库。一种选择是使用 jackson-dataformat-xmljackson-module-scala 。这也意味着您必须编写自己的自定义 Marshaller。幸运的是,这并不难。

这里有一些可能适合您的简单代码:

import akka.http.scaladsl.marshalling.{ToResponseMarshallable, Marshaller}

// json marshalling
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._
import spray.json.DefaultJsonProtocol._
implicit val departmentFormat = DefaultJsonProtocol.jsonFormat1(department)
val departmentJsonMarshaller = SprayJsonSupport.sprayJsonMarshaller[department]

// xml marshalling, need to write a custom Marshaller
// there are several MediaTypes for XML such as `application/xml` and `text/xml`, you'll have to choose the one you need.
val departmentXmlMarshaller = Marshaller.StringMarshaller.wrap(MediaTypes.`application/xml`)((d: department) => {
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
val mapper = new XmlMapper()
mapper.registerModule(DefaultScalaModule)
mapper.writeValueAsString(d)
})


private val route =
(pathPrefix("employee") & get) {
path(Segment) { id =>
parameters('department ? "") { (flag) =>
extractUri { uri => {
flag match {
case "hr" => {
// explicitly use the XML marshaller
complete(ToResponseMarshallable(department(name = flag))(departmentXmlMarshaller))
}
case "tech" => {
// explicitly use the JSON marshaller
complete(ToResponseMarshallable(department(name = flag))(departmentJsonMarshaller))
}
}
}
}
}
}
}

请注意,为了让 Jackson XML 序列化器正常工作,department 类应该是顶级类,否则您将收到有关错误根名称的神秘错误。

关于scala - akka http 使用 Json 支持和 xmlsupport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51247460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com