【问题标题】:Grails: Unable to resolve class groovyx.net.http.HTTPBuilderGrails:无法解析类 groovyx.net.http.HTTPBuilder
【发布时间】:2013-02-25 23:53:27
【问题描述】:

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

我已经看到我需要使用 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 服务?

提前致谢。

【问题讨论】:

  • 您在安装插件后尝试过grails refresh-dependenciesgrails clean 吗?我曾经自己尝试过,在我安装 rest 插件后一切都启动并运行(顺便说一句。在 BuildConfig.groovy 文件中声明插件而不是通过命令行安装它们并拥有插件是一种“更好”的做法)依赖项写入application.properties 文件:) )。
  • 我尝试执行 grails refresh-dependencies 和 grails clean 但错误仍然存​​在。我没有在 BuildConfig.groovy 中声明插件,因为我是 Grails 的新手,我不知道该怎么做。
  • 这些天你不应该使用grails install-plugin,而是应该在pluginsBuildConfig.groovy 部分添加一个依赖项(compile ":rest:0.7"

标签: web-services grails post request httpbuilder


【解决方案1】:

所以,我通读了您发布的Exception 和您的代码 sn-p,您似乎在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,因为代码解释得很好,并且还列出了一些操作方法和教程;)

【讨论】:

  • 感谢您的解释和您分享的链接。它运行良好。
  • 很高兴我能帮助你 - 继续努力:D
【解决方案2】:

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

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

希望对您有所帮助。 BR

【讨论】:

  • groovy_install_directory/lib 和 grails 项目中的 webapp/app/lib 一样吗?
  • 该链接现已断开。看起来 codehaus.org 已经不复存在了。
猜你喜欢
  • 2018-08-10
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多