Che*_*bye 2 grails post web-services request httpbuilder
我开发了一些我想在grails应用程序中使用的Web服务.可以使用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
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是在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
Run Code Online (Sandbox Code Playgroud)
我已经使用以下命令安装了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'
}
}
Run Code Online (Sandbox Code Playgroud)
但这并不能解决问题.
有关信息,我使用netbeans 7.2.1和Grails 2.2.0.
我的代码有问题,还是有简单的方式来请求Web服务?
提前致谢.
所以,我Exception再次通读了你发布的和你的代码片段,似乎你已经req在http.request(){}闭包中省略了变量.您还没有导入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
}
}
Run Code Online (Sandbox Code Playgroud)
我也建议在这个位置阅读HTTPBuilder的文档:http://groovy.codehaus.org/modules/http-builder/doc/index.html,因为代码很好地解释了,还列出了一些howtos和教程;)
| 归档时间: |
|
| 查看次数: |
19235 次 |
| 最近记录: |