我正在學習一個名爲geoscript-groovy的腳本包的groovy。我跟着時髦REST教程here和測試下面的代碼:Groovy:無法解決類groovyx.net.http.RESTClient
import groovyx.net.http.RESTClient
def client = new RESTClient('http://www.acme.com/')
def resp = client.get(path : 'products/3322') // ACME boomerang
不過,我在import
聲明得到了一個錯誤說:
Groovy:unable to resolve class groovyx.net.http.RESTClient
我,四處搜尋,有很多的問題和答案對於此錯誤消息,例如,import groovyx.net.http.RESTClient in Groovy class和RestClient Grails Import fails。然而,他們都是爲了Grails的,我不使用它,也不是很熟悉。
我的問題是
我應該如何解決這個錯誤,如果我只有groovy? (我的版本的groovy使用以下命令安裝在Ubuntu 12.04下)。
sudo apt-add-repository ppa:groovy-dev/groovy
sudo apt-get update
sudo apt-get install groovy
謝謝。
- 編輯---
我加入建議@Grab陳述,並提出了一個兩行rest1.groovy
文件,如下所示:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
import groovyx.net.http.RESTClient
groovyConsole rest1.groovy
似乎運行正常。但groovysh < rest1.groovy
仍然給我一個錯誤(如下所示)。我想我需要在類似groovysh
的環境中運行,因爲groovy腳本在後臺被稱爲Web服務。沒有@Grab
行,該服務會生成一個異常。隨着@Grab
線,該服務甚至不會註冊。是否有一種比每個腳本抓取包含groovyx.net.http.RESTClient
的必要依賴關係更持久的方法(例如apt-get
或手動複製某些內容)?
groovysh < rest1.groovy
Groovy Shell (1.8.6, JVM: 1.7.0_72)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000> @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
groovy:001> import groovyx.net.http.RESTClient
ERROR org.codehaus.groovy.tools.shell.CommandException:
Invalid import definition: 'import groovyx.net.http.RESTClient'; reason: startup failed:
script1413902882282760571375.groovy: 1: unable to resolve class groovyx.net.http.RESTClient
@ line 1, column 1.
import groovyx.net.http.RESTClient
你在你的類路徑中包含http-builder嗎? 看看這個例子。 http://groovy.codehaus.org/modules/http-builder/doc/rest。html – Raphael 2014-10-20 23:10:27