【问题标题】:Groovy RESTClient returns inconsistent type on response handlerGroovy RESTClient 在响应处理程序上返回不一致的类型
【发布时间】:2013-10-01 18:52:06
【问题描述】:

我正在尝试为 Groovy 的 RESTClient(围绕 HttpBuilder)编写自己的响应处理程序。如果返回响应正文,我想始终打印响应正文。但是,我找不到一致的方法来做到这一点。

自定义响应处理程序通常如下所示:

def client = new RESTClient(url)

client.handler.success = { resp, reader ->
     //do stuff
}

client.handler.failure = { resp, reader ->
     //do stuff
     throw new Exception("HTTP call failed. Status code: ${resp.getStatus()}")
}

但是,我注意到变量“reader”可以根据响应具有不同的类。我见过读者类型为groovy.util.slurpersupport.NodeChildorg.apache.http.conn.EofSensorInputStream。我希望它是一个可预测的类,所以我实际上可以调用这个对象的方法。这是怎么回事?

【问题讨论】:

    标签: groovy httpbuilder


    【解决方案1】:

    将内容类型设置为 ANY 并将 HttpBuilder 内容解析器更改为文本解析器解决了该问题。响应处理程序中reader 的类型现在始终为java.io.InputStreamReader

    之前:

    def headerMap = [:]
    //populate headers
    def response = client.get("headers":headerMap)
    

    之后:

    client.parser.'application/xml' = client.parser.'text/plain'
    client.parser.'application/xhtml+xml' = client.parser.'text/plain'
    client.parser.'application/atom+xml' = client.parser.'text/plain'
    client.parser.'application/json' = client.parser.'text/plain'
    client.parser.'text/html' = client.parser.'text/plain'
    client.parser.'application/x-www-form-urlencoded' = client.parser.'text/plain'
    def headerMap = [:]
    //populate headers
    def response = client.get("headers":headerMap, contentType:groovyx.net.http.ContentType.ANY)
    

    【讨论】:

      猜你喜欢
      • 2022-07-29
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多