开发者

renderJson without template

I have controller with

render(messages);

And i have route

GET     /                                       Application.index

I want to implement some rest features, and add this route

GET     /api/index                              Application.index(format:'json')

I have template not found exception. How can i say to play use renderJSON() when for开发者_运维百科mat is json without any code changes?


As you use the render() method, Play! will search a template file with the name of the action (detail on Play! website : http://www.playframework.org/documentation/1.2.2/controllers#template).

You have to use renderJSON(params...), it will bypass the default template!


Your use case doesn't really make sense. In the standard render() call, you are likely passing some pojos to the template to use (or none at all), this is a varargs method. In the renderJSON() call you always need to pass an object which can be serialized to json by the Gson library, or a string with is already in json.

Add a new method to your Application class that handles json responses:

# normal index page
GET     /             Application.index
# api request
GET     /api/index    Application.indexJson
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜