0

I'm having a very simple restful controller, which looks like this:

class PersonController extends RestfulController<Person> {

    static responseFormats = ['json', 'xml']

    PersonController() {
        super(Person)
    }
}

However, now I want to add a search option to this. What is the Grails way of making this possible?

I thought of adding the following:

def search(Map params) {
    println params
}

But that makes Grails (2.3) crash (| Error Fatal error during compilation org.apache.tools.ant.BuildException: Compilation Failed (Use --stacktrace to see the full trace)).

So what is the right way of adding this? I'm looking for some solution which I can call using http://localhost:8080/foo/person/search?q=erik

This is my UrlMappings:

static mappings = {
    "/$controller/$action?/$id?(.${format})?"{
        constraints {
            // apply constraints here
        }
    }


    "/rest/persons"(resources:'Person')

I've changed the above to:

def search() {
    println params
}

And that doesn't give the compilation error anymore, but I still get this error:

TypeMismatchException occurred when processing request: [GET] /declaratie-web/rest/medicaties/search - parameters:
q: erik
Provided id of the wrong type for class nl.Person. Expected: class java.lang.Long, got class java.lang.String. Stacktrace follows:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class nl.Person. Expected: class java.lang.Long, got class java.lang.String

I also found out that it doesn't matter how I call the controller:

http://localhost:8080/foo/person/search?q=erik
http://localhost:8080/foo/person/search222?q=erik
http://localhost:8080/foo/person/search39839329?q=erik

All fails with the above error, so it seems my method is ignored (maybe caused by my URLmapping?)

3
  • Did you try to leave away the params argument of the search action? Commented Oct 14, 2013 at 13:02
  • Hi aioslos, thanks for the hint. I tried that, and Grails starts up now, but it doesn't work. I also added the UrlMapping, maybe that's the cause? What I found out is that the controller is completely ignoring the 'search' part right now, I can replace it by a dummy word and it will still give an error. Commented Oct 14, 2013 at 13:26
  • /foo/person/search... does not follow the REST model and you shouldn't use a RestfulController if you don't want a REST api. Commented Oct 14, 2013 at 13:31

2 Answers 2

2

You really aren't being RESTful by doing that. q should just be a parameter for the index action. You can override that method to include your functionality.

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    def c = Person.createCriteria()
    def results = c.list(params) {
       //Your criteria here with params.q
    }
    respond results, model:[personCount: results.totalCount]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, sure, that makes so much sense! Thanks so much for helping out here, that's of course the solution.
You can add String q to the input parameters of the action instead of doing params.q if you wish.
0

@james-kleeh solution is right, but you can do it more clean by override the listAllResources method which is called by index

@Override
protected List<Payment> listAllResources(Map params) {
    Person.createCriteria().list(params) {
        // Your criteria here with params.q
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.