You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Rudy Bonefas <ru...@gmail.com> on 2008/09/05 04:13:26 UTC

REST, JAXB and Collection based return types question

I believe I read in a previous post on Nabble as well as in the
documentation that RESTful services cannot return Collections, in particular
generic collections like List<MyClass>.  I have a few questions concerning
this:

1.  Can someone explain WHY this is so.  I have a base service
implementation that is both a JAXWS service and a JAXRS service.  JAXWS has
no problem returning a collection. I'd prefer not to have to change the
return type just to satisfy my REST needs.

2. The User doc mentions several alternatives to creating a custom wrapper
class for the collection, but does not provide any examples of the
alternatives, in particular


   - "one can provide an Object factory which will tell JAXB how to marshal
   a given type (in case of Collections - its template type)"  This sounds like
   just what the doctor ordered.  Can anyone provide an example of this and
   will it somehow screw up my JAXWS calls
   - "Another option is to register one or more JAX-RS ContextResolver
   providers capable of creating JAXBContexts for a number of different types.
   The default JAXBElementProvider will check these resolvers first before
   attempting to create a JAXBContext on its own.+ -- Not sure if this will do
   what I need, but I'd like a code example if possible.

Finally, if neither of the above are what I'm looking for, any other
ideas/code patches?

Thanks in advance

Rudy Bonefas
Next Century Corp.
Senior Code Monkey

Re: REST, JAXB and Collection based return types question

Posted by Sergey Beryozkin <se...@iona.com>.
Hi Rudy


>I believe I read in a previous post on Nabble as well as in the
> documentation that RESTful services cannot return Collections, in particular
> generic collections like List<MyClass>.  I have a few questions concerning
> this:
>
> 1.  Can someone explain WHY this is so.  I have a base service
> implementation that is both a JAXWS service and a JAXRS service.  JAXWS has
> no problem returning a collection. I'd prefer not to have to change the
> return type just to satisfy my REST needs.

Supporting explicit parameterized return types (like Collection<Bar>) is on the map for JAX-RS.
Realistically it will be looked after the 1.0-final of the JAX-RS api is supported (most of it at least).
We're probably talking about the end of the year - unless some contributions are made.
For now some additional work on behalf of a user is needed.

>
> 2. The User doc mentions several alternatives to creating a custom wrapper
> class for the collection, but does not provide any examples of the
> alternatives, in particular
>
>
>   - "one can provide an Object factory which will tell JAXB how to marshal
>   a given type (in case of Collections - its template type)"  This sounds like
>   just what the doctor ordered.  Can anyone provide an example of this and
>   will it somehow screw up my JAXWS calls

I think if you provide an ObjectFactory which will have a method creating a JAXBContext for
say List<MyClass> then it would work. Unfortunately I do not know much about JAXB advanced techniques -hopefully, if you go with 
this option you'll get more help either on this list or on a JAXB list.

>   - "Another option is to register one or more JAX-RS ContextResolver
>   providers capable of creating JAXBContexts for a number of different types.
>   The default JAXBElementProvider will check these resolvers first before
>   attempting to create a JAXBContext on its own.+ -- Not sure if this will do
>   what I need, but I'd like a code example if possible.

See http://blogs.sun.com/japod/entry/better_json_available_in_jersey

It's very similar to the approach above : implement say MyClassContextResolver<JAXBContext>
and return a dedicated JAXBContext for MyClass...This resolver would need to be registered

>
> Finally, if neither of the above are what I'm looking for, any other
> ideas/code patches?

Another option is to add a method-level annotation, see for ex :

http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/server/HelloWorld.java

and check the actual adapter implementation in the same demo - I hope JAX-RS runtime will eventually create such adapters on the 
fly...

Yet another option is to register a custom provider for say List<MyClass> and do some custom un(marshalling), possibly bypassing 
JAXB altogether

Let us know please if either of these options will work for you.



Cheers, Sergey

>
> Thanks in advance
>
> Rudy Bonefas
> Next Century Corp.
> Senior Code Monkey
> 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland