dev@jersey.java.net

Re: [Jersey] JSONWithPadding does not serialize

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 28 Oct 2010 13:13:22 +0200

On Oct 28, 2010, at 4:46 AM, Vidit Drolia wrote:

> Hi,
>
> I am trying to return a JSONP object with Spring 2.6 and Jersey 1.4.
> My resource looks like the following:
>
> @Produces({"application/xml", "application/json","application/x-
> javascript"})
> public JSONWithPadding getMethod(@Context UriInfo info) {
>
> Obj flr = new Obj('some-response');
> return new JSONWithPadding(flr,"foobar");
> }
>
> With the above setup, I get a response in both xml and json. However,
> I am unable to get a response as a JSONP object. Is there anything I
> am missing in the above setup?

Looks OK to me.


> Please do let me know if I should
> provide any other info to figure this out.
>

What happens when the client sends an accept header of "application/x-
javascript"?

What client are you using?

One way this could occur is if you are using the browsers default
accept header to make async requests then it is likely that XML is the
most preferable. If you want the "application/x-javascript" to take
priority if it is acceptable then you can do this:

   @Produces({"application/xml", "application/json","application/x-
javascript;qs=2"})
   public JSONWithPadding getMethod(@Context UriInfo info) {
         Obj flr = new Obj('some-response');
         return new JSONWithPadding(flr,"foobar");
   }

i.e. use Jersey's quality of source feature.

Paul.