【问题标题】:Grails & Nexmo pluginGrails & Nexmo 插件
【发布时间】:2015-10-07 06:09:18
【问题描述】:

我在集成 Nexmo 插件的 Grails 中遇到错误。发生错误的文件是'NexmoService.groovy',如下:

package grails.plugin.nexmo

import groovyx.net.http.HTTPBuilder
import org.springframework.context.i18n.LocaleContextHolder as LCH

import static groovyx.net.http.ContentType.URLENC
import static groovyx.net.http.Method.POST

class NexmoService {

    def grailsApplication
    def messageSource

    def sendSms(String to, String text, String from=config?.sms?.default_from) throws NexmoException {
        if (!to || !text || !from) {
            throw new NexmoException(getMessage("nexmo.sms.error.missing.param"))
        }

        def http = new HTTPBuilder(config?.endpoint)  
        def requestBody = [to: to, text: text, from: from, api_key: config?.api?.key, api_secret: config?.api?.secret]

        http.request(POST) {
            uri.path = "/sms/${config?.format}"
            send(URLENC, requestBody)

            response.success = { resp, data ->
                def message = data?.messages[0]
                def statusCode = message?.status
                if (statusCode != "0") {
                    def error = getMessage("nexmo.sms.status.${statusCode}", [message?."error-text"], getMessage("nexmo.sms.error.default"))
                    throw new NexmoException(error)
                }
                log.info(getMessage("nexmo.sms.success"))
                return [status: message?.status, id: message?."message-id"]
            }
            response.failure = { resp, data ->
                def error = getMessage("nexmo.sms.error.response", [resp?.status], getMessage("nexmo.sms.error.default"))
                throw new NexmoException(error)
            }
        }
    }

    def call(String to, String text, String from="") throws NexmoException {
        if (!to || !text) {
            throw new NexmoException(getMessage("nexmo.call.error.missing.param"))
        }

        def http = new HTTPBuilder(config?.endpoint)
        def requestBody = [to: to, text: text, from: from, api_key: config?.api?.key, api_secret: config?.api?.secret]

        http.request(POST) {
            uri.path = "/tts/${config?.format}"
            send(URLENC, requestBody)

            response.success = { resp, data ->
                def statusCode = data?.status
                if (statusCode != "0") {
                    def error = getMessage("nexmo.call.status.${statusCode}", [data?."error-text"], getMessage("nexmo.call.error.default"))
                    throw new NexmoException(error)
                }
                log.info(getMessage("nexmo.call.success"))
                return [status: data?.status, id: data?."call-id"]
            }
            response.failure = { resp, data ->
                def error = getMessage("nexmo.call.error.response", [resp?.status], getMessage("nexmo.call.error.default"))
                throw new NexmoException(error)
            }
        }
    }

    private ConfigObject getConfig() {
        return grailsApplication.config?.nexmo
    }

    private String getMessage(String code, List args=[], String defaultMessage="") {
        if (messageSource.resolveCode(code, LCH.locale)) {
            return messageSource.getMessage(code, args.toArray(), LCH.locale)
        }
        return defaultMessage
    }
}

错误如下:

| Error 编译错误:启动失败: C:\nexmo-master\grails-app\services\grails\plugin\nexmo\NexmoService.groovy: 3:无法解析类groovyx.net.http.HTTPBuilder@第3行, 第1栏.导入groovyx.net.http.HTTPBuilder ^

C:\nexmo-master\grails-app\services\grails\plugin\nexmo\NexmoService.groovy: 6:无法解析类 groovyx.net.http.ContentType @ line 6, 第1栏.导入静态groovyx.net.http.ContentType.URLENC ^

C:\nexmo-master\grails-app\services\grails\plugin\nexmo\NexmoService.groovy: 7:无法解析类groovyx.net.http.Method@第7行,列 1.导入静态groovyx.net.http.Method.POST ^

3 个错误

【问题讨论】:

  • 您似乎没有获得 Nexmo 插件需要和包含的 HTTPBuilder。也许 Nexmo 插件与您当前的 Grails 版本已过时?您可以尝试通过将 REST 插件包含到您的构建配置中来自己包含它。

标签: grails plugins nexmo


【解决方案1】:

类路径中没有 http-builder 库。查看这篇文章以获取导入此库的说明。 How to import groovyx.net.http 或 http://www.groovy-lang.org/mailing-lists.html#nabble-td3995735

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多