0

I have an empty project created using GGTS, my app runs fine, I can see the views and navigate through them fine.

I need con call a REST api and consume the data provided, I read and added the necessary plug in to the BuildConfig file under the dependencies and then under the plugins section, none of them seem to work.

compile ":rest-client-builder:2.0.0"

I've created a controller, I'm not sure where to add the import here, so I've tried:

package myapp


class UserController {

    def index() {
        import grails.plugins.rest.client.RestBuilder
        String url = "https://foo.com/batch/$id"
        def resp = new RestBuilder().get(url) { header 'Authorization', 'Basic base64EncodedUsername&Password' }

        render resp
    }

but I get the error: unable to resolve class....

The API is returning JSON data, what I;ve done so far is just to create a new Grails project, add controller, add view and then the dependency.

I've cleaned an built the project several times but the error remains.

Thanks for your help.

fabian278
  • 115
  • 1
  • 1
  • 10
  • This thread should be helpful: [Cannot reference plugin classes grails 2.4.4](https://stackoverflow.com/questions/32173091/cannot-reference-plugin-classes-grails-2-4-4/) – Mike Sw May 10 '18 at 00:26

2 Answers2

0

Import goes at the top

After the package line

And before the class line

Also, depending on your version of grails, you might want to move of ggts... Not sure it's supported any more...

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks, I put the import there but I get unable to resolve class grails.plugins.rest.client.RestBuilder now, same problem as before. What would you recommend instead of ggts? thanks!! – fabian278 Apr 08 '17 at 20:28
0

I think that the problem is in your build.gradle dependency definition.

Insert the next line to your build.gradle file and I think that will be helpful:

compile "org.grails.plugins:rest-client-builder:2.1.1"

For more information follow the grails plugin documentation.

Please be aware that all the business logic should be placed in the service layer and not in the controller. It is a best practice to keep your controller lean as possible.

Another best practice is to define the following line:

String url = "https://foo.com/batch/$id"

in the beggining of the controller as static final String

static final String url = "https://foo.com/batch/" 

and add the $id when needed in the code.

Here is how that should be called inside the method:

String url = "${url}/{$id}"
Rotem
  • 1,381
  • 1
  • 11
  • 23
  • Rotem, thanks for your answer, this is my first Grails app and I don't have any idea of how it works. I added the line on the buildconfig.groovy file, I can't find the build.grandle file anywhere on my project. Also, I'm not sure where to add the business logic, is it a domain class or just a regular class? – fabian278 Apr 09 '17 at 17:44
  • In that case you are not familiar with the gradle building tool, I would recommend reading the following documentation, to illuminate a little bit about gradle. https://docs.gradle.org/current/userguide/tutorial_java_projects.html. the build.gardle file should be set under the webapp directory. If you are not familiar with services, leave it for now, it is a good practice but not mandatory. – Rotem Apr 09 '17 at 17:50