2

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.

Chewbye
  • 263
  • 1
  • 8
  • 20
  • did you try a `grails refresh-dependencies` and `grails clean` after you've installed the plugin? I once tried it myself and everything was up and running after I installed the `rest` plugin (btw. it is a 'better' practice to declare the plugins within the `BuildConfig.groovy` file instead of installing them through the command line and have plugin dependencies written to the `application.properties` file :) ). – herom Feb 26 '13 at 06:53
  • I tried to do grails refresh-dependencies and grails clean but the error is still here. I didn't declare plugin in BuildConfig.groovy because I am new with Grails and I don't know how to do that. – Chewbye Feb 26 '13 at 12:41
  • 1
    You shouldn't use `grails install-plugin` these days, instead you should put a dependency (`compile ":rest:0.7"`) in the `plugins` section of your `BuildConfig.groovy` – Ian Roberts Feb 26 '13 at 14:34

2 Answers2

5

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 ;)

herom
  • 2,532
  • 29
  • 41
3

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

Mendas
  • 31
  • 3