overwrite renderJSON method for JSONP support

852 views
Skip to first unread message

Christian Koch

unread,
Aug 19, 2011, 3:49:35 PM8/19/11
to play-fr...@googlegroups.com
Hello together,

instead of an easy renderJSON call I have to check in every action wether it is a JSONP call. This works with this 

    public static void index() {

       List<Note> notes = Note.findAll();

       if (request.params._contains("callback")) {

          Gson gson = new Gson();

          String out = gson.toJson(notes);

          renderText(request.params.get("callback") + "(" + out + ")");

       } else {

          renderJSON(notes);

       }

    }

Is it possible to overwrite the renderJSON() method? If I do so Play recognizes this as a new action.


Thank you


Christian

Julien Richard-Foy

unread,
Aug 20, 2011, 5:33:23 AM8/20/11
to play-fr...@googlegroups.com
> Is it possible to overwrite the renderJSON() method? If I do so Play
> recognizes this as a new action.

Use the @Util annotation to prevent this behavior.

Christian Koch

unread,
Aug 21, 2011, 5:56:08 AM8/21/11
to play-fr...@googlegroups.com
Thank you!

Is there a plan for implementing Jsonp in Play directly.


Christian

Erwan Loisant

unread,
Aug 22, 2011, 3:47:28 AM8/22/11
to play-fr...@googlegroups.com
On Sun, Aug 21, 2011 at 11:56, Christian Koch <koch....@gmail.com> wrote:
> Thank you!
>
> Is there a plan for implementing Jsonp in Play directly.

Not currently, but that's an interesting suggestion.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/NUGKqwe91B0J.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

--
Erwan Loisant

Christian Koch

unread,
Aug 22, 2011, 3:57:08 AM8/22/11
to play-fr...@googlegroups.com
As far as I understand it should be possible to provide that as a Play module? perhaps with as an annotation @JsonpEnabled ;-)

Christian

Erwan Loisant

unread,
Aug 22, 2011, 4:12:59 AM8/22/11
to play-fr...@googlegroups.com
Well, you could provided as a module but the code is so simple that
I'm not sure it's worth the pain - you can just copy/paste the file in
all your projects.

I you want to reuse it in several controllers, you can just create a
JsonP class and use the @With annotation to include it in any
controller.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/onvkBK7MBBwJ.

Christian Koch

unread,
Aug 22, 2011, 4:52:19 AM8/22/11
to play-fr...@googlegroups.com
But if I create a controller in package controllers like this

package controllers;


import com.google.gson.Gson;


import play.mvc.*;


/**

 * Enable JSONP for Play controllers

 * @author ckoch

 *

 */

public class Jsonp extends Controller {


/**

* renders a callback function if there

* is a request parameter callback

* @param o

*/

    @Util

    public static void renderJSON(Object o) {

    if (request.params._contains("callback")) {

    Gson gson = new Gson();

    String out = gson.toJson(o);

    renderText(request.params.get("callback") + "(" + out + ")");

    } else {

    renderJSON(o);

    }

    }

    

    /**

* renders a callback function if there

* is a request parameter callback

     * 

     * @param str

     */

    @Util

    public static void renderJSON(String str) {

    if (request.params._contains("callback")) {

    renderText(request.params.get("callback") + "(" + str + ")");

    } else {

    renderJSON(str);

    }

    }


}


it won't work using the @With(Jsonp.class) annotation. If I copy the two methods into my Application.java controller it works. 

Any ideas?

Julien Richard-Foy

unread,
Aug 22, 2011, 4:59:48 AM8/22/11
to play-fr...@googlegroups.com
You can use these static methods like this:

Jsonp.renderJSON(…)

BTW, you should set the content-type to "text/javascript" when returning jsonp.

Christian Koch

unread,
Aug 22, 2011, 5:12:56 AM8/22/11
to play-fr...@googlegroups.com
ok, but what's the problem with the @With annotation?

Jsonp.renderJSON(…)

BTW, you should set the content-type to "text/javascript" when returning jsonp.

Right, that's what Firebug is telling me all the day ;-)

Christian 

Julien Richard-Foy

unread,
Aug 22, 2011, 5:23:11 AM8/22/11
to play-fr...@googlegroups.com
> ok, but what's the problem with the @With annotation?

Actually you don’t need it. Unless I’m mistaking, Erwan?

Roman R

unread,
Nov 21, 2012, 11:27:46 AM11/21/12
to play-fr...@googlegroups.com
Thanks for this example!
I subclassed the Controller and simply put these methods into chain:

    public static void renderJSON(String str) {

    if (request.params._contains("callback")) {

    renderText(request.params.get("callback") + "(" + str + ")");

    } else {

    Controller.renderJSON(str);

    }

    }


Just needed Controller.renderJSON() instead of renderJSON() there.
Reply all
Reply to author
Forward
0 new messages