users@jersey.java.net

Re: [Jersey] json-from-jaxb example

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 28 Oct 2008 17:53:36 +0100

Hi Tyson,

Hmm... this is tricky. There is no explicit functionality to support
collections of JAXB beans. It is all encapsulated in the message body
readers/writers, which you can access via Providers [1]. But this
requires you have a Jersey set up running.

Would i be correct in saying that you want functionality on the
JSONJAXBContext for marshal/unmarshal collections?

Paul.

https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/ext/Providers.html

On Oct 28, 2008, at 5:36 PM, Tyson Norris wrote:

> Thanks for the response Paul.
>
> What we are doing, may be thought of as using the ContextResolver
> outside the context of Jersey resource method calls. For example, I
> can configure a ContextResolver as a spring bean, and then look up
> and use that spring bean elsewhere.
>
> This is a handy way to use a single approach (JAXB configs) to
> manage conversion of POJO to JSON string. Some of these conversions
> are handled by Jersey, some are manually invoked in application logic.
>
> Jersey does this automatically for resource method signature types,
> and Collections of JAXB classes work in that case. What I’m not sure
> how to do, is make the ContextResolver work this way for Collections
> of JAXB types that are NOT automatically handled by Jersey (since
> these Collections of JAXB types are being used outside of any Jersey
> resource methods). It works fine to use ContextResolver in this way
> for non-Collections.
>
> Another way to think of this situation is that we are trying to use
> some of the Jersey infrastructure as a generic (read: used in cases
> outside of Jersey resource method calls) way to generate JSON format
> strings based on JAXB configuration.
>
> Thanks!
> Tyson
>
>
>
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: Tuesday, October 28, 2008 1:16 AM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] json-from-jaxb example
>
> 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
>