从HTTPResponse闭包Grails HTTPBuilder返回值到外部方法

时间:2013-08-23 21:43:54

标签: grails closures grails-controller httpbuilder

我有一些像这样的代码

def lookupTickets() {
    User currentUser = webAuthService.currentUser()
    def http = new HTTPBuilder(zdURL)
    http.auth.basic("${zdUser}/token", zdApiKey)
    http.get(path: "/api/v2/users/search.json", 
             query: [query: currentUser.emailAddress], 
             requestContentType: ContentType.JSON, { resp, json ->
              println "Response status: ${resp.statusLine}"
                  def zenDeskUserId = json?.users[0]?.id
    })
    return MYRESULT
}

def zenDeskUserId = json?.users[0]?.id行给了我想要返回浏览器的结果。

如果只在内部闭包内的范围内,我怎么能在外部方法中返回这个值?

1 个答案:

答案 0 :(得分:2)

你觉得这不行吗?

def lookupTickets() {
    def zenDeskUserId

    User currentUser = webAuthService.currentUser()
    def http = new HTTPBuilder(zdURL)
    http.auth.basic("${zdUser}/token", zdApiKey)
    http.get(path: "/api/v2/users/search.json", 
             query: [query: currentUser.emailAddress], 
             requestContentType: ContentType.JSON, { resp, json ->

                 println "Response status: ${resp.statusLine}"
                 zenDeskUserId = json?.users[0]?.id
    })
    return zenDeskUserId
}