简体   繁体   中英

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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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