How to call Service in Grails with groovy

时光怂恿深爱的人放手 提交于 2020-01-04 18:16:05

问题


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.


回答1:


If you want to call the plugin from a service method, you would need to do:

  1. change the name of your service (so it isn't called SipgateService)
  2. Add the def sipgateService as a class definition, not a method one

Does this work?

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"
  }
}

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()
    ...
  }
}


来源:https://stackoverflow.com/questions/6251912/how-to-call-service-in-grails-with-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!