users@jersey.java.net

Re: JSONp with Jersey 1.4?

From: Dominique Guinard <dguinard_at_ethz.ch>
Date: Thu, 11 Nov 2010 14:20:09 -0500

Hi Jakub,

> Please try to wrap your org.epcis.fosstrak.restadapter.model.Resource
> instance with com.sun.jersey.api.json.JSONWithPadding
> as demonstrated in example [1]. You will still
> be able to get JSON response, when needed,
> but will be also capable of generating the JavaScript as requested.
I completely overlooked the fact the JSONWithPadding wasn't a custom
MessageBodyWriter!

I used it as a wrapper like you suggested and it now runs fine:

   public JSONWithPadding getBusinessLocations(UriInfo context) {
[...]
         return new JSONWithPadding(resource);

Thanks a lot!

Dominique

-- Dominique Guinard, Web of Things Researcher
MIT Auto-ID labs
www.guinard.org
-- 
On 11/11/2010 01:21 PM, Jakub Podlesak wrote:
> Hi Dominique,
>
> Please try to wrap your org.epcis.fosstrak.restadapter.model.Resource
> instance with com.sun.jersey.api.json.JSONWithPadding
> as demonstrated in example [1]. You will still
> be able to get JSON response, when needed,
> but will be also capable of generating the JavaScript as requested.
>
> HTH,
>
> ~Jakub
>
> [1]http://download.java.net/maven/2/com/sun/jersey/samples/jsonp/1.4/jsonp-1.4-project.zip
>
>
> On 11/10/2010 01:56 AM, dguinard_at_ethz.ch wrote:
>> Dear Jersey people,
>>
>> We are currently in the process of opensourcing a framework for RFID
>> tracking based on Jersey.
>>
>> I wanted to add JSONp support before going live.
>> I read in a number of mails on this list that a jsonp response is
>> triggered by:
>>
>> (Paul Sandoz):
>> "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")));"
>>
>> I added this to my resources:
>>
>> @Path(EPCIS_ROOT)
>> @Produces({Config.TEXT_HTML_WEBKIT_SAFE, APPLICATION_JSON,
>> APPLICATION_XHTML_XML, TEXT_XML, "application/x-javascript"})
>> public class RESTfulEPCIS implements ICaptureResource, IQueryResource,
>> IRESTfulEPCISResource, ISubscriptionResource {
>>
>> but whenever I try a cURL (I'm actually using Poster but anyways) for
>> application/javascript I trigger an exception:
>>
>> javax.ws.rs.WebApplicationException:
>> com.sun.jersey.api.MessageException: A message body writer for Java
>> class org.epcis.fosstrak.restadapter.model.Resource, and Java type
>> class
>> org.epcis.fosstrak.restadapter.model.Resource, and MIME media type
>> application/x-javascript was not found
>> at
>> com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.
>> java:278)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl._handleReques
>> t(WebApplicationImpl.java:1326)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest
>> (WebApplicationImpl.java:1239)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest
>> (WebApplicationImpl.java:1229)
>> at
>> com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.
>> java:420)
>> at
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletCo
>> ntainer.java:497)
>> at
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletCo
>> ntainer.java:684)
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applic
>> ationFilterChain.java:290)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFil
>> terChain.java:206)
>> at
>> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperVal
>> ve.java:233)
>> at
>> org.apache.catalina.core.StandardContextValve.invoke(StandardContextVal
>> ve.java:191)
>> at
>> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.jav
>> a:127)
>> at
>> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.jav
>> a:102)
>> at
>> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve
>> .java:109)
>> at
>> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
>> 298)
>> at
>> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:8
>> 59)
>> at
>> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process
>> (Http11Protocol.java:588)
>> at
>> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
>> at java.lang.Thread.run(Thread.java:636)
>> Caused by: com.sun.jersey.api.MessageException: A message body writer
>> for Java class org.epcis.fosstrak.restadapter.model.Resource, and Java
>> type class org.epcis.fosstrak.restadapter.model.Resource, and MIME
>> media
>> type application/x-javascript was not found
>>
>> Any idea what's going on? Did I get it wrong? Isn't the JSONp writer
>> triggered automatically?
>>
>> I'm deploying the app in Tomcat (6.28).
>>
>> Thanks a lot for your help,
>>
>> Jersey rocks ;-)
>>
>> -- Dominique Guinard, Web of Things Researcher
>> MIT Auto-ID labs
>> www.guinard.org
>
-- 
_____________________________________________________________________
  ETH Zurich, Computer Science, Institute for Pervasive Computing,
  Distributed Systems Group
  PhD Candidate, MSc in Computer Science.
  Dominique Guinard                http://www.guinard.org
  ETH-Zentrum, CNB H 107.2         Phone: +41 (0) 44 632 6136
  Universitaetstrasse 6            Fax:   +41 (0) 44 632 1659
  CH-8092 Zurich, Switzerland
____________________________________________________________________