users@jersey.java.net

Re: [Jersey] JSONP Callback support

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Wed, 04 Feb 2009 12:06:06 -0500

Farrukh Najmi wrote:
> Farrukh Najmi wrote:
>> Jakub Podlesak wrote:
>>> On Wed, Feb 04, 2009 at 11:30:21AM +0100, Paul Sandoz wrote:
>>>
>>> <snip/>
>>>
>>>>> The use of '@' sign in resulting JSON as described by BadgerFish
>>>>> is causing a downstream problem for me as the toolkit I am using
>>>>> to process JSON does not like it. So I need to fix my
>>>>> ResponseFilter to remove '@' sign in the JSON output.
>>>
>>> You do not need to filter '@' out using ResponseFilter,
>>> please see [1] for two options how to get rid of '@' and still
>>> retain possibility to de-serialize the JSON back to Java.
>>
>> I created a class as follows using NATURAL notation and am using
>> latest svn bit for 1.0.2-SNAPSHOT.
>> I am still getting getting '@' chars in field names. Do I need to do
>> something else in terms of configuration
>> to tell jersey runtime to use this Provider (I am guessing that is
>> what @Provider annotation is already doing).
>>
>> Any idea what I could be missing?
>>
>> Note I am using a contextPath rather than specific classes which is
>> morer tedious to do.
>>
>>
>> @Provider
>> public final class JAXBContextResolver implements
>> ContextResolver<JAXBContext> {
>>
>> private final JAXBContext context;
>>
>> public JAXBContextResolver() throws Exception {
>> this.context = new JSONJAXBContext(
>> JSONConfiguration.getBuilder(JSONConfiguration.Notation.NATURAL).build(),
>> BindingUtility.jaxbContextPath);
>> }
>>
>> public JAXBContext getContext(Class<?> objectType) {
>> return context;
>> }
>> }
>>
>>
>> Thanks very much for all you help.
>>
> Paul pointed out the problem is because I use spring to configure
> jersey components therefor
>
> <paul>
> Because you are using Spring then by default Jersey will let Spring
> define the components and it will not search by default.
> If you want to intermix Spring and non Spring-components you need to
> configure Jersey to scan. Use the package config
> init-param for that
>
> See:
> https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/package-summary.html
>
> </paul>
>
> Thanks Paul! I am trying this out.
>

My Provider was being found once I did what Paul suggested and added the
com.sun.jersey.config.property.packages param to my web.xml for the
jersey SpringServlet:

  <servlet>
    <servlet-name>Registry REST Interface</servlet-name>
    
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
      
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
      <param-value>mypkg.rest.JSONCallbackResponseFilter</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>mkpkg.rest</param-value>
    </init-param>
  </servlet>

However, I then got the following server startup error:

SEVERE: The provider class, class mypkg.rest.JAXBContextResolver, could
not be instantiated
javax.xml.bind.JAXBException: property "retainReferenceToInfo" is not
supported
        at
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:114)
        at
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:188)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:133)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:286)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
        at
com.sun.jersey.api.json.JSONJAXBContext.<init>(JSONJAXBContext.java:342)
        at
org.freebxml.omar.server.interfaces.rest.JAXBContextResolver.<init>(JAXBContextResolver.java:26)

Paul pointed out this is likely due to my using an older version of
JAXB. Jersey needs :

<dependency>
  <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.1.10-SNAPSHOT</version>
</dependency>

I will report back on my results in a bit. Thanks very much to Paul and
Jakub for their help.

-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com