2013-02-25 54 views
1

我已經開發了一些我希望在我的grails應用程序中使用的web服務。 可以使用Get或POST協議調用這些服務。Grails:無法解析類groovyx.net.http.HTTPBuilder

我看到我需要使用HTTP builder對象來做到這一點。

這是我的代碼:

import groovyx.net.http.HTTPBuilder 
import groovyx.net.http.ContentType 
import groovyx.net.http.Method 
import groovyx.net.http.RESTClient 
import groovyx.net.http.HttpResponseDecorator 
     def http = new HTTPBuilder('http://localhost:8086') 
     http.request(GET, JSON) { 
      uri.path = '/RegistrationService/webresources/users/isRegistered' 
      uri.query = [ login:'aa', password: 'bb' ] 

      response.success = { resp, xml -> 
      def xmlResult = xml 
      } 
     } 

我的問題是,在Netbeans的,我爲每個進口的錯誤: 無法解析類groovyx.net.http.HTTPBuilder 無法解析類groovyx .net.http.ContentType ...

但是我試着運行應用程序,這是錯誤,當我跑我的代碼:

| Error 2013-02-25 23:33:32,596 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [POST] /WordGame/user/authenticate 
No such property: uriPath for class: groovyx.net.http.HTTPBuilder$RequestConfigDelegate. Stacktrace follows: 
Message: No such property: uriPath for class: groovyx.net.http.HTTPBuilder$RequestConfigDelegate 
    Line | Method 
->> 21 | doCall in wordgame.UserController$_closure2_closure4 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 425 | doRequest in groovyx.net.http.HTTPBuilder 
| 359 | request . in  '' 
|  19 | doCall in wordgame.UserController$_closure2 
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
|  63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter 
| 1110 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 603 | run  in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 722 | run . . . in java.lang.Thread 

我已經使用以下命令安裝了其餘插件:grails install-plugin rest 而且我已經嘗試使用NetBeans界面安裝它,它告訴我它已正確安裝。

我看到的一些論壇上,我需要有像依賴該文件BuildConfig.groovy在:

dependencies { 
    runtime('org.codehaus.groovy.modules.http-builder:http-builder:0.5.1') { 
     excludes 'xalan' 
     excludes 'xml-apis' 
     excludes 'groovy' 
    } 
} 

但這不是解決問題。

有關我正在使用netbeans 7.2.1和Grails 2.2.0的信息。

我的代碼有問題嗎?還是有更簡單的方法來請求Web服務?

在此先感謝。

+0

在安裝插件後,您是否嘗試過'grails refresh-dependencies'和'grails clean'?我曾經自己嘗試過,並且在安裝'rest'插件後,所有東西都運行起來了(順便說一下,在BuildConfig.groovy文件中聲明插件而不是通過命令行安裝它們是一種更好的做法,有寫入到'application.properties'文件的插件依賴項:))。 – herom 2013-02-26 06:53:39

+0

我試圖做grails刷新依賴和grails乾淨,但錯誤仍然在這裏。我沒有在BuildConfig中聲明插件。因爲我是Grails新手,我不知道該怎麼做。 – Chewbye 2013-02-26 12:41:01

+1

你現在不應該使用'grails install-plugin',而應該在你的'BuildConfig.groovy'的'plugins'部分放置一個依賴項('compile「:rest:0.7」') – 2013-02-26 14:34:45

回答

5

因此,我通過您發佈的Exception和您的代碼片段再次閱讀,似乎您已在http.request(){}閉包中省略req變量。還沒有導入GET方法和TEXT內容類型。嘗試:

import groovyx.net.http.HTTPBuilder 
//import groovyx.net.http.ContentType // this doesn't import ContentType 
//import groovyx.net.http.Method // this doesn't import Method 
import groovyx.net.http.RESTClient 
import groovyx.net.http.HttpResponseDecorator 

// ContentType static import 
import static groovyx.net.http.ContentType.* 
// Method static import 
import static groovyx.net.http.Method.* 

     def http = new HTTPBuilder('http://localhost:8086') 
     http.request(GET, JSON) { req -> // 'req ->' is not present in your code snippet! 
      uri.path = '/RegistrationService/webresources/users/isRegistered' 
      uri.query = [ login:'aa', password: 'bb' ] 

      response.success = { resp, xml -> 
      def xmlResult = xml 
      } 
     } 

還我會建議通過HTTPBuilder在這個位置的文檔閱讀:http://groovy.codehaus.org/modules/http-builder/doc/index.html的代碼是很好的解釋和一些HOWTO文檔和教程還列出;)

+0

感謝你的支持解釋和你分享的鏈接。它運作良好。 – Chewbye 2013-02-26 16:45:40

+0

很高興我可以幫助你 - 繼續增加:D – herom 2013-02-26 17:40:37

相關問題