简体   繁体   English

如何在 groovy 脚本中返回 class 成员字符串

[英]How to return a class member string in groovy script

I have the following code snipet我有以下代码片段

class **ResultToken** {
 String token
 String expiration
}


// HTTP post request to retrive active token
// Return : ResultToken object
ResultToken getToken(){
ResultToken token

http.request(POST) {

    ...

    response.success = { resp, json ->
    token = new ResultToken(token: json["access_token"].toString(), 
expiration: json["expires_in"].toString())

    }
}    
token
}

def tokenValue =getToken().token
return tokenValue

Exception error: groovy.lang.MissingPropertyException: No such property: http for class: Script259 at Script259.getToken(Script259.groovy:21) at Script259.run(Script259.groovy:41) Exception error: groovy.lang.MissingPropertyException: No such property: http for class: Script259 at Script259.getToken(Script259.groovy:21) at Script259.run(Script259.groovy:41)

Any idea?任何想法?

regards问候

This way you define the handler which doesn't return anything usefull.通过这种方式,您可以定义不返回任何有用的处理程序。

It should rather be:它应该是:

ResultToken getToken(){
  ResultToken token

  http.request(POST) {
    ....
    response.success = { resp, json ->
      token = new ResultToken(token: json.access_token, expiration: json.expires_in)
    }
  }

  token
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM