Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从httpbuilder ng get请求获取原始json字符串或客户jackson映射器_Json_Rest_Groovy_Httpbuilder_Httpbuilder Ng - Fatal编程技术网

从httpbuilder ng get请求获取原始json字符串或客户jackson映射器

从httpbuilder ng get请求获取原始json字符串或客户jackson映射器,json,rest,groovy,httpbuilder,httpbuilder-ng,Json,Rest,Groovy,Httpbuilder,Httpbuilder Ng,我正在将groovy脚本从更新到。这些脚本与各种Web服务交互以获取消息。我希望更好地使用Jackson将响应解析为对象。然而,我似乎无法像在httpbuilder中那样获得原始json响应,因为httpbuilder ng在所有情况下都会自动解析为lazymap 使用httpbuilder的旧实现允许您使用body.text方法获取原始json字符串,而无需将其解析为LazMap。然后可以将其与Jackson中的ObjectMapper一起使用,以创建我的POJO。然而,httpbuilder

我正在将groovy脚本从更新到。这些脚本与各种Web服务交互以获取消息。我希望更好地使用Jackson将响应解析为对象。然而,我似乎无法像在httpbuilder中那样获得原始json响应,因为httpbuilder ng在所有情况下都会自动解析为lazymap

使用httpbuilder的旧实现允许您使用body.text方法获取原始json字符串,而无需将其解析为LazMap。然后可以将其与Jackson中的ObjectMapper一起使用,以创建我的POJO。然而,httpbuilder ng似乎并不支持这一点

我已经在这里尝试了获取原始json的方法,但是body.text方法在我使用的1.03版本中似乎不起作用

我还尝试添加自己的自定义编码器来覆盖Groovy默认创建的JSON对象,但没有成功。据推测,这是可能的,详情见维基。如果任何人有一个代码片段如何做到这一点,将不胜感激

旧的httpbuilder代码:

def http = new HTTPBuilder("http://${proxy}.domain.ie")

    http.request(GET, TEXT) {
        uri.path = "/blah/plugins/blah/queues"
        headers.Accept = 'application/json'
        headers.'Cookie' = "token=${token}"

        response.success = { resp, json ->
            assert resp.status < 300
            LOGGER.info("GET queues request succeeded, HTTP " + resp.status)

            ObjectMapper objectMapper = new ObjectMapper()

            queues = objectMapper.readValue(json, Queue[].class)
        }

        response.failure = { resp ->
            assert resp.status >= 300
            LOGGER.info("GET queues request failed, HTTP " + resp.status)
            return null
        }
    }
更新

找到了解决办法。使用FromServer.inputstream.text方法添加自定义解析器和闭包

def text = httpBuilder.get {
        request.uri.path = '/blah/plugins/blah/queues'
        response.parser('application/json') { ChainedHttpConfig cfg, FromServer fs ->
            String text = fs.inputStream.text
        }
    }
def text = httpBuilder.get {
        request.uri.path = '/blah/plugins/blah/queues'
        response.parser('application/json') { ChainedHttpConfig cfg, FromServer fs ->
            String text = fs.inputStream.text
        }
    }