में परिभाषित मान के साथ एक स्थैतिक चर प्रारंभ करें config.groovy
में परिभाषित मान के साथ static
चर प्रारंभ कैसे कर सकता हूं? (कई मिलता है, पोस्ट, डाल दिया और DELETE)Grails: config.groovy
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}")
...
}
}
मैं प्रत्येक विधि के अंदर http
चर को परिभाषित नहीं करना चाहती:
वर्तमान में मैं कुछ इस तरह की है।
मैं http
वैरिएबल static
सेवा के अंदर वैरिएबल के रूप में रखना चाहता हूं।
मैं सफलता के बिना इस की कोशिश की:
class ApiService {
static grailsApplication
static http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
JSON get(String path) {
http.get(...)
...
}
}
मैं Cannot get property 'config' on null object
मिलता है। के साथ एक ही:
class ApiService {
def grailsApplication
static http
ApiService() {
super()
http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
}
JSON get(String path) {
http.get(...)
...
}
}
इसके अलावा, मैं एक static
परिभाषा के बिना की कोशिश की, लेकिन एक ही त्रुटि Cannot get property 'config' on null object
:
class ApiService {
def grailsApplication
def http
ApiService() {
super()
http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
}
}
कोई सुराग?
धन्यवाद इयान! एक जादू की तरह काम करता है :) – Agorreca