我有一项服务,我有一个方法可以调用,我如何访问该服务。 我已经看到短信插件并安装了它,以及如何将短信从我的应用程序发送到不同的手机。我遵循了 grails 短信插件,但我没有得到任何结果 ecxept ecxeptions
class SipgateService {
static transactional = true
def serviceMethod() {
def sipgateService
//def phoneNumber = 'XXXXXXXXXX' //phoneNumber according to E.164 specification
//working alternative:
println "service"
def phoneNumber = 'XXXXXXXXXX'
def result = sipgateService.sendSMS(phoneNumber, 'This is my Text to send!')
result ? 'Sending Successful':'Sending failed'
println "after service"
}
}
请举个例子来解释一下。 提前非常感谢。
最佳答案
如果您想从服务方法调用插件,您需要执行以下操作:
- 更改您的服务名称(使其不再名为
SipgateService
) - 将
def sipgateService
添加为类定义,而不是方法
这有效吗?
class MySMSService {
static transactional = true
def sipgateService // This will be injected from the SMS plugin
def serviceMethod() {
println "service"
def phoneNumber = 'XXXXXXXXXX'
def result = sipgateService.sendSMS(phoneNumber, 'This is my Text to send!')
result ? 'Sending Successful':'Sending failed'
println "after service"
}
}
然后,从 Controller 中,在类级别定义到 MySMSService
的链接,并调用您的 serviceMethod
方法,即:
class MyController {
def mySMSService // this will be injected from your service
// then, when you want to use it (from an action)
def someAction = {
...
mySMSService.serviceMethod()
...
}
}
关于grails - 如何使用 groovy 调用 Grails 中的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6251912/