web服务-Grails:无法解析groovyx.net.http.HTTPBuilder类



我已经开发了一些web服务,希望在我的grails应用程序中使用。可以使用Get或POST协议调用这些服务。

我已经看到我需要使用HTTP构建器对象来实现这一点。

这是我的代码:

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.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

我使用以下命令安装了rest插件: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服务吗?

提前谢谢。

因此,我再次阅读了您发布的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由于代码解释得很好,还列出了一些操作指南和教程;(

我刚刚解决了我的问题。下载此:http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/0.5.2/

从groovy_installed_directory/lib中的dependencies目录复制httpBuilder.jar和所有jar。然后重新启动控制台并重试。

希望能有所帮助。BR

最新更新