Thaldin
Thaldin

Reputation: 283

Passing an instance of a class in Groovy using generics

Grails: 2.4.4 Groovy: 2.4.1 Java : 1.8u40

Windows 7

I am trying to make a generics based controller that members of my team can extend as needed.

Having just getting this issue solved in Groovy with generics ( Why doesn't this generic usage work in groovy?)

I am now running into the following issue in a Grails controller trying to pass an instance of the class.

The TeamCotroller:

class TeamController extends BaseController<Team> {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    TeamController() {
        super(Team)
    }

    /*
    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Team.list(params), model:[teamInstanceCount: Team.count()]
    }
    */
    /*
    def show(Team teamInstance) {
       respond teamInstance
    }
    */
    /*
    def create() {
        respond new Team(params)
    }
    */

    /* More default CRUD methods cut out for now */
}

The Generic Controller: class BaseController {

    private final Class<T> clazz

    BaseController(Class<T> clazz) {
        this.clazz = clazz
    }

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond clazz.list(params), model:[instanceCount: clazz.count()]
    }

    // TODO: Figure this out
    def show(Team instance) {
        respond instance
    }
}

In the show(Team instance) method, I have replaced Team with Class<T> and T in an attempt to get the instance being passed to it by Grails/Groovy but it doesn't even seem to be hitting the method at all when I run in debug mode. What does one need to do in order to get the instance being passed to the controller?

--edit-- Adding Exception 2015-03-06 15:56:36,400 [http-bio-8090-exec-5] ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [GET] /test-list/team/show/1 No such property: instance for class: TeamController. Stacktrace follows: Message: No such property: instance for class: TeamController

-- Edit -- Adding controller code

Upvotes: 0

Views: 1991

Answers (1)

blacktide
blacktide

Reputation: 12076

It doesn't seem to work when passing the instance directly into the parameters of the method, but it does work as expected when using the id parameter, like so:

grails-app/controllers/BaseController.groovy

abstract class BaseController<T> {

  private final Class<T> clazz

  BaseController() {}

  BaseController(Class<T> clazz) {
    this.clazz = clazz
  }

  def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond clazz.list(params), model:[instanceCount: clazz.count()]
  }

  def show(Long id) {
    def instance = clazz.get(id)
    respond instance
  }
}

grails-app/controllers/TeamController.groovy

class TeamController extends BaseController<Team> {

    TeamController() {
        super(Team)
    }
}

Then you should be able to access the /team/show/1.?{format} endpoint without an issue.

I setup an example project here to demonstrate.

Upvotes: 1

Related Questions