Grails JSON renderer not exposing model for Interceptor

217 views Asked by At

I want to use the model field in an Interceptor for a JSON REST response.

The controller method responds with the domain object:

def show(String id) {
    respond User.get(id)
}

The JSON view uses JsonViewJsonRenderer which extends DefaultViewRenderer, which directly calls

  view.render(model, request, response)

and doesn't to expose a ModelAndView instance. Other renderers e.g. DefaultHtmlRenderer, expose the model via:

 applyModel(context, object)

Is there a way to get the model field to be filled out when responding with a JSON response? Do I need to build my own subclass of DefaultHtmlRenderer that sets the ModelAndView?

As a workaround, I could create a ModelAndView for each response, but this seems to not be in the ease-of-use spirit of Grails:

def show(String id) {
    respond new ModelAndView('user/show', User.get(id))
}
1

There are 1 answers

3
Jeff Scott Brown On

Is there a way to get the model field to be filled out when responding with a JSON response?

Yes. There is a model property available in all interceptors. Typically this would be accessed in the after() method of an interceptor.

Do I need to build my own subclass of DefaultHtmlRenderer that sets the ModelAndView?

No.