0

我正在“管道”从外部服务返回的 json 提要(在某些情况下相当大),以隐藏客户端的访问 api 密钥(访问密钥是该服务唯一可用的身份验证系统)。

我正在使用 Gaelyk,我写了这个 groovlet:

try {
    feed(params.topic)
} catch(Exception e) {
    redirect "/failure"
}

def feed(topic) {

    URL url = new URL("https://somewhere.com/$topic/<apikey>/feed")
    def restResponse = url.get()

    if (restResponse.responseCode == 200) {
        response.contentType = 'application/json'
        out << restResponse.text
    }
}

唯一的问题是“restResponse”非常大,并且 groovlet 返回的值被截断。所以我会得到一个这样的json:

[{"item":....},{"item":....},{"item":....},{"ite

如何在不截断的情况下返回完整的 json?

4

1 回答 1

0

好吧,我找到了解决方案,问题出在开头(URL 内容必须作为流读取)。所以内容被截断它不是输出而是输入:

def feed(topic) {
    URL url = "https://somewhere.com/$topic/<apikey>/feed".toURL()
    def restResponse = url.get()

    if (restResponse.responseCode == 200) {
        response.contentType = 'application/json'
        StringBuffer sb = new StringBuffer()
        url.eachLine {
            sb << it
        }
        out << sb.toString()
    }
}
于 2014-07-04T06:30:40.023 回答