users@jersey.java.net

Re: [Jersey] JSONP

From: Felipe Gaścho <fgaucho_at_gmail.com>
Date: Thu, 21 Jan 2010 10:19:58 +0100

a jsonp example:

http://kenai.com/projects/puj/sources/arena/content/arena-http/src/main/java/com/kenai/puj/arena/http/PujAdvertisementResource.java?rev=429

    @GET
    @Path("jsonp")
    @Produces( { "application/x-javascript", MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML })
    public JSONWithPadding selectAllByRoleP(
            @QueryParam("jsoncallback") @DefaultValue("loadAds")
String callback,
            @QueryParam(EntityFacadeConstants.PARAM_MAX)
@DefaultValue(EntityFacadeConstants.PARAM_MAX_DEFAULT_VALUE) int max,
            @QueryParam("role") String role,
            @QueryParam("comp") String competition) {

        return new JSONWithPadding(
                new GenericEntity<Collection<PujAdvertisementEntity>>(adsFacade
                        .getAdvertisement(competition, role, max)) {
                }, callback);

    }


a jQuery script consuming this method:

http://fgaucho.dyndns.org:8080/arena-dwr/

just check the source code, at the bottom of the document, look for
something like "jQuery.noConflict();"

* the jsonp resources should return a type application/x-javascript

any questions, just ask.....

On Thu, Jan 21, 2010 at 10:08 AM, Jakub Podlesak <Jakub.Podlesak_at_sun.com> wrote:
> On Wed, Jan 20, 2010 at 06:27:45PM +0000, Paul Sandoz wrote:
>>
>> On Jan 20, 2010, at 5:46 PM, Jakub Podlesak wrote:
>>
>> >
>> >Hi Ben,
>> >
>> >Now i know what is going wrong in this case.
>> >You do not get any exception nor error, but just a wrong
>> >strange JSON data back, right?
>> >
>> >JSONWithPadding is accompanied with it's own JSON writer.
>> >But this writer is never picked up, as the Jackson one takes
>> >precedence.
>> >
>> >Please confirm this is the behavior you are experiencing,
>> >i.e. instead of e.g. {"one":"apple", "two":"onion"},
>> >which represents the map, you getting st. like:
>> >
>> >{"jsonSource":{"one":"apple", "two":"onion"},
>> >"callbackName","callback"}
>> >
>>
>> JSONWithPadding writing is triggered for the following media types:
>>
>>         javascriptTypes.put("application", new
>> HashSet<String>(Arrays.asList("x-javascript", "ecmascript",
>> "javascript")));
>>         javascriptTypes.put("text", new
>> HashSet<String>(Arrays.asList("ecmascript", "jscript")));
>
> Yes, but in addition, it delegates to a JSON provider, if application/json
> is asked, and gives it (the JSON provider found) only the JSON payload
> (from the wrapper) to serialize out.
> The problem is, the Jackson writer comes first and serializes
> not only the payload, but the whole JSONWithPadding wrapper.
>
> Since the Jackson writer is enabled by the application via the META-INF/services,
> it is given higher priority then the JSONWithPaddingProvider, and the delegation
> never happens.
>
> But even when this was configured centrally within Jersey, since there is AFAIK
> no way to set priority of message body readers/writers, this could still be an issue.
>
> I do not know what would be the right solution at the moment. The application
> could e.g.define an @Provider annotated writer, not configured via the META-INF/services
> mechanism, which would proxy the JSONWithPadding provider. But i do not think that
> is the proper way.
>
> Anyway, as the workaround, instead of just one single @GET annotated resource method
>
> @GET @Produces({"application/json","application/javascript"})
> public JSONWithPadding getIt() { ... }
>
> one needs to define two methods:
>
> @GET @Produces("application/json")
> public Object getJSON() { ... }
>
> @GET @Produces("application/javascript")
> public JSONWithPadding getJavascript() { return new JSONWithPadding(getJSON()); }
>
> Maybe, it just was not a greet idea to let the JSONP provider serialize JSON.
>
> ~Jakub
>
>>
>> So if one of those is more acceptable than application/json then you
>> will get JavaScript back with JSON embedded in a function.
>>
>> Paul.
>>
>>
>> >Right?
>> >
>> >~Jakub
>> >
>> >
>> >On Wed, Jan 20, 2010 at 05:01:41PM +0100, Jakub Podlesak wrote:
>> >>On Wed, Jan 20, 2010 at 10:16:04AM -0500, Ben Darfler wrote:
>> >>>Forgive this question as it might not make sense yet but I'm
>> >>>trying to
>> >>>wade through this.
>> >>
>> >>No problem, your question makes perfect sense.
>> >>
>> >>>
>> >>>I'm currently using jackson as the json provider for jersey. We have
>> >>>an interest in supporting JSONP and I'm wondering if there is a
>> >>>way to
>> >>>do this. I've found JSONWithPadding class in Jersey that seems to
>> >>
>> >>The JSONWithPaddingWrapper is exactly what you need.
>> >>
>> >>>suggest a way of doing this but I'm not sure of the syntax.
>> >>>Previous I
>> >>>would just return a map from my restful method and jackson/jersey
>> >>>would take care of the serialization. However, if I change the
>> >>>method
>> >>>to return new JSONWithPadding(map) it fails and I'm not easily
>> >>>able to
>> >>>figure out why. My gut says that I need to add JAXB annotations to
>> >>>my
>> >>
>> >>From your description it seems, you use the wrapper the way it was
>> >>intended.
>> >>There should be no need for any extra annotations. What kind of
>> >>failure are you getting?
>> >>Any exception stack trace?
>> >>
>> >>>objects which I have been able to avoid so far. Is there any way of
>> >>>doing jsonp with jackson as the json provider for jersey? (whew
>> >>>thats
>> >>
>> >>It is possible that you revealed a bug in the JSONP support in
>> >>Jersey,
>> >>what kind of map do you use? I need to add a test for this, as so far
>> >>only the JAXB based scenario has been tested.
>> >>
>> >>>a lot of buzz words).
>> >>>
>> >>>I asked the jackson mailer and they are interested in helping out on
>> >>>this if anything needs to be done on their side.
>> >>
>> >>Thanks for the info, i will contact Tatu, if there is a need.
>> >>
>> >>~Jakub
>> >>
>> >>>
>> >>>http://archive.codehaus.org/lists/org.codehaus.jackson.user/msg/5f7770581001191408m3659e3e4w608b294ce6041947@mail.gmail.com
>> >>>
>> >>>Ben
>> >>>
>> >>>
>> >>>--
>> >>>Software Engineer
>> >>>LocaModa
>> >>>Connecting People & Places
>> >>>
>> >>>---------------------------------------------------------------------
>> >>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> >>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
>> >>>
>> >>
>> >>--
>> >>http://blogs.sun.com/japod
>> >>
>> >>---------------------------------------------------------------------
>> >>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> >>For additional commands, e-mail: users-help_at_jersey.dev.java.net
>> >>
>> >
>> >--
>> >http://blogs.sun.com/japod
>> >
>> >---------------------------------------------------------------------
>> >To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> >For additional commands, e-mail: users-help_at_jersey.dev.java.net
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>



-- 
------------------------------------------
   Felipe Gaścho
   10+ Java Programmer
   CEJUG Senior Advisor