如何使用Scalaxb异步客户端

时间:2019-07-04 07:27:47

标签: xml scala soap scalaxb sbt-scalaxb

在我当前的项目中,我必须实现一个客户端来调用SOAP服务。我选择使用ScalaXB,以便从WSDL文件生成客户端及其模型。我设法编译了项目并生成了模型,但是我仍然无法进行HTTP调用。

这是我的DispatchHttpClientsAsync特质,问题是我实际上如何进行Http调用?

trait DispatchHttpClientsAsync extends HttpClientsAsync {
  lazy val httpClient = new DispatchHttpClient {}
  // https://github.com/AsyncHttpClient/async-http-client/blob/1.9.x/src/main/java/com/ning/http/client/AsyncHttpClientConfigDefaults.java
  def requestTimeout: Duration = 60.seconds
  def connectionTimeout: Duration = 5.seconds

  trait DispatchHttpClient extends HttpClient {
    import dispatch._

    // Keep it lazy. See https://github.com/eed3si9n/scalaxb/pull/279
    lazy val http = Http.configure(_.
      setRequestTimeout(requestTimeout.toMillis.toInt).
      setConnectTimeout(connectionTimeout.toMillis.toInt))

    def request(in: String, address: java.net.URI, headers: Map[String, String])(implicit ec: ExecutionContext): Future[String] = {
      val req = url(address.toString).setBodyEncoding("UTF-8") <:< headers << in
      http(req > as.String)
    }
  }
}

1 个答案:

答案 0 :(得分:0)

DispatchHttpClientsAsync只是内在特质。您可以调用的实际客户端实现在其他位置生成。我不知道您的SOAP服务及其操作的名称,但是请看WSDL示例http://scalaxb.org/wsdl-support

  

这将生成以下9个文件:

     
      
  • scalaxb / httpclients_async.scala
  •   
  • scalaxb / httpclients_dispatch_async.scala
  •   
  • scalaxb / scalaxb.scala
  •   
  • scalaxb / soap12_async.scala
  •   
  • soapenvelope12 / soapenvelope12.scala
  •   
  • soapenvelope12 / soapenvelope12_xmlprotocol.scala
  •   
  • stockquote / stockquote.scala
  •   
  • stockquote / stockquote_type1.scala
  •   
  • stockquote / xmlprotocol.scala
  •   

看看stockquote /文件夹,您的SOAP服务应该有一个特征和一个方法。在示例中,您应该扩展特征StockQuoteSoap12Binding-存在必需方法的实现。