1

I have a Grails 3.3.8 app with

  compile 'org.grails.plugins:views-json'
  compile 'org.grails.plugins:views-json-templates'

and a json view under views/partner/_companion.gson:

import io.my.domain.Companion

model {
  Companion companion
}
json {
  id companion.id
  name companion.name
  description companion.description
}

Now I want to render my object to json inside the controller action:

class PartnerController {
  def action() {
    Companion o = Companion.get(...)
    log.info render( view:"/partner/_companion", model:[ companion:o ] )
    log.info render( view:"_companion", model:[ companion:o ] )
    log.info render( template:"companion", model:[ companion:o ] )
    log.info render( template:"companion", model:o )
  }
}

All of those calla output nulls.

What is the propper way of rendering?

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • You are invoking the `render` method at https://github.com/grails/grails-core/blob/v3.3.8/grails-plugin-controllers/src/main/groovy/grails/artefact/controller/support/ResponseRenderer.groovy#L216. That has a `void` return type which is why your output is showing `null`. That method doesn't return a value. It renders the template to the response. – Jeff Scott Brown Nov 29 '19 at 16:47
  • `g.render()` wouldn't also work without TagLib plugin, would it? – injecteer Nov 29 '19 at 23:13
  • That is right. Even with `g.render()` several of the approaches you show in your code sample wouldn't work. `view:"/partner/_companion"`, `view:"_companion"` and `model:o` are each invalid. – Jeff Scott Brown Dec 04 '19 at 16:10
  • @JeffScottBrown I think they would work, as I copied them from the working controller with web-profile. Anyway, do you have some lib in mind similar to `json-view` plugin but able to work outside Grails? – injecteer Dec 04 '19 at 17:39
  • "Anyway, do you have some lib in mind similar to json-view plugin but able to work outside Grails?" - It depends. Would it be a JVM app? – Jeff Scott Brown Dec 04 '19 at 18:54
  • yes, JVM like Micronaut or Vert.x – injecteer Dec 04 '19 at 20:26

0 Answers0