2

I found that Grails 3.2+ supports json views which provides an elegant way for the response to have json.

So is there a way such that instead of having the ".json" in the request URL, we render a custom JSON template from the controller. I tried using the render method, but keep getting an error that the template could not be found. Additionally reading through the documentation I can't seem to find a way to do this. Is this something that is supported yet?

4

2 回答 2

1

在 url 中包含 .json 只是另一种指定接受内容类型的方法。您可以改为指定标题。

有关如何使用 json 视图的说明,请阅读文档:http: //views.grails.org/latest/

class Controller {
    def index() {
        [foo: new Foo()]
    }
}

/grails-app/views/controller/index.gson

@Field Foo foo

json g.render(foo)
于 2017-02-27T06:12:03.277 回答
0

您可以使用 render(json:"path") 从控制器渲染任意 gson 视图。

SomethingController{

def giveMeJson(){
      render(json:"my_template",model:[some_data:some_instance])
}

}
于 2019-12-19T04:52:02.350 回答