users@jersey.java.net

Re: [Jersey] json-from-jaxb example

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 28 Oct 2008 09:15:43 +0100

Hi Tyson.

Collections of JAXB classes are supported.

See the JAXB sample for XML:

http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.0/jaxb-1.0-project.zip

Although this is for XML it should also work for JSON.

Paul.


On Oct 27, 2008, at 10:10 PM, Tyson Norris wrote:

> Hi –
> We have some JSON format services created, which are all working
> well, based on the json-from-jaxb example.
>
> Is there anyway to get the JSON format string of a Collection<?>
> type using the ContextResolver injected via @Context? We can do it
> with JAXB types, but I’m trying to figure out if we can avoid
> wrapping the collections.
>
> Background:
> We have some occasions where we need to “manually” generate JSON
> format strings and use a String type result on a resource method,
> which works fine when we modify the types in our JAXBContextResolver
> class.
>
> We would like to be able to also “manually” generate JSON format
> strings from Collection<?> types, but I’m not sure if this is
> possible?
>
> The method:
> JAXBContext getContext(Class<?> objectType)
>
> Only receives type info for the collection instance, not the type of
> its contents.
>
> Any ideas on a way around this?
>
> Our alternative will be to not rely on JAXB config to generate JSON
> format in these cases, which is less than ideal.
>
> Our use case for doing this is that we have some JSP templates that
> serve HTML + javascript, and the javascript uses some AJAX style
> widgets that consume JSON data. In some cases, it is best for us to
> serve all the content at one time, where the JSON data is embedded
> into the HTML as a static string in JSON format.
>
>
> Thanks for any ideas!
>
> Tyson