Question

I have developped some web services that I would like tu use in my grails application. These services can be called using Get or POST protocols.

I have seen that I need to use the HTTP builder object to do that.

This is my code:

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

The problem I have is that in Netbeans I have an error for each import: Unable to resolve class groovyx.net.http.HTTPBuilder Unable to resolve class groovyx.net.http.ContentType ...

However I tried to run the application and this is the error when I run my code:

| 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

I have installed the rest plugin using the command: grails install-plugin rest And I have already tried to install it with the netbeans interface and it tells me that it is correctly instaled.

I have seen on some forums that I need to had dependencies in the file BuildConfig.groovy like that:

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

But this is not resolving the problem.

For information I am using netbeans 7.2.1 and Grails 2.2.0.

Is there something wrong with my code or is there a simplier way to request a web service?

Thanks in advance.

Was it helpful?

Solution

so, I read through the Exception you posted and your code snippet again and it seems that you have omitted the req variable within the http.request(){} closure. also you have not imported the GET method and TEXT content type. try:

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

also I would recommend to read through the documentation of HTTPBuilder at this location: http://groovy.codehaus.org/modules/http-builder/doc/index.html as the code is well explained and some howtos and tutorials are also listed ;)

OTHER TIPS

i just solved my problem. Download this: http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/0.5.2/

Copy the httpBuilder.jar and all the jars from dependencies directory in your groovy_installed_directory/lib. Then restart console and try again.

Hope it helps. BR

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top