如何用Groovy在Grails中调用服务 [英] How to call Service in Grails with groovy
问题描述
我有一项服务,因为我有一种方法可以打电话,我该如何访问此服务。
我看到了短信插件,并安装它,我怎么能从我的应用程序发送短信到不同的手机。我跟着grails短信插件,但我没有得到任何结果ecxept ecxeptions
I have one service in that i have a method to call and how can i acces this service. I have seen sms plugin and installed it and How can i send sms from my application to the different mobiles.I followed the grails sms plugin but i didn't get any results 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"
}
}
请用一个例子来解释我。
非常感谢。
Please explain me with an example. thanks alot in advance.
推荐答案
如果您想从服务方法调用插件,您需要请执行以下操作:
$ b
If you want to call the plugin from a service method, you would need to do:
- 更改您的服务的名称(因此它不被称为
SipgateService
) - 添加
def sipgateService
作为类定义,而不是一个方法
- change the name of your service (so it isn't called
SipgateService
) - Add the
def sipgateService
as a class definition, not a method one
这是否工作?
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"
}
}
然后,从控制器中,在类级别定义到 MySMSService
的链接,并调用您的 serviceMethod
方法即:
Then, from a controller, define the link to MySMSService
at class level, and call your serviceMethod
method ie:
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()
...
}
}
这篇关于如何用Groovy在Grails中调用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!