Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails: initialize a static variable with a value defined in config.groovy
    primarykey
    data
    text
    <p>How can I initialize a <code>static</code> variable with a value defined in <code>config.groovy</code>?</p> <p>Currently I have something like this:</p> <pre class="lang-js prettyprint-override"><code>class ApiService { JSON get(String path) { def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") ... } JSON get(String path, String token) { def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") ... } ... JSON post(String path, String token) { def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") ... } } </code></pre> <p>I don't want to define the <code>http</code> variable inside each method (several GET, POST, PUT and DELETE). </p> <p>I want to have the <code>http</code> variable as a <code>static</code> variable inside the service.</p> <p>I tried this without success:</p> <pre class="lang-js prettyprint-override"><code>class ApiService { static grailsApplication static http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") JSON get(String path) { http.get(...) ... } } </code></pre> <p>I get <code>Cannot get property 'config' on null object</code>. The same with:</p> <pre class="lang-js prettyprint-override"><code>class ApiService { def grailsApplication static http ApiService() { super() http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") } JSON get(String path) { http.get(...) ... } } </code></pre> <p>Also I tried without a <code>static</code> definition, but the same error <code>Cannot get property 'config' on null object</code>:</p> <pre class="lang-js prettyprint-override"><code>class ApiService { def grailsApplication def http ApiService() { super() http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}") } } </code></pre> <p>Any clue?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload