cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Kulp <dk...@apache.org>
Subject Re: @XmlRootElement with jax-rs/jax-ws
Date Wed, 05 Aug 2009 15:42:49 GMT

There may be a couple of options worth investigating.   I'm not familiar 
enough with the way JAXB works with JAX-RS to really say whether this will 
work or not:

1) Return JAXBElement<Book>, not just Book.   With jaxb, when marshalling, you 
either have to hand it a class with an XmlRootElement annotation or you need 
to hand it a JAXBElement.    This should make it go the second route.

2) To make (1) more explicit, when you run xjc, there IS a jaxb customization 
to have it actually generate a class for each root element.  I think its 
<jaxb:globalBindings generateElementClass="true"/>

With JAX-WS in "Bare" mode, we wouldn't create wrapper classes.   However, in 
that case, we have the @WebResult annotation to help us define the element 
name to use.   Thus, the runtime will create a JAXBElement with the QName 
created from the @WebResult and set the value of the JAXBElement to the "Book" 
and feed it to the JAXB runtime.   That's how it works with JAXWS.    For JAX-
RS, I'm not sure there is really any place to record the namespace/name for 
the wrapper element to use as an annotation.   I suppose we could allow 
@XmlElement on the method for the return, but until we go JAXB 2.2 which would 
allow it on the params as well, there wouldn't be anything we can do for the 
incoming side (or the outgoing side for the client stuff).


Dan


On Wed August 5 2009 4:39:59 am Sergey Beryozkin wrote:
> Hi,
>
> AFAIK, in case of JAXWS, additional JAXB elements/classes are generated.
> For ex, if it's a method called 'getBook' returning Book then it will be a
> getBookResponse class that will be generated.
>
> I was also assuming that if ObjectFactory had methods returning
> JAXBElements then there'd be no need in updating the generated classes with
> @XmlRootElement which apparently is not the case.
>
> I don't think that in case of JAX-RS we can generate wrappers too.
> Dan - is there anything that can be done at a pure JAXB level for users to
> avoid explicitly adding XMLRootElement ? It appears there's no solution
> really...
>
> thanks, Sergey
>
> Scott Oster-2 wrote:
> > I am trying to add jax-rs annotations to an existing service
> > implementation
> > generated by wsdltojava (wrapped style).  The POJO returned by my
> > operation
> > is defined in a standalone XSD, and understandably doesn¹t have the
> > @XmlRootElement annotation (for the reasons described here
> > http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html
> >).
> >
> > My web service works fine, but my REST call dies with the error:
> > unable to marshal type "..." as an element because it is missing an
> > @XmlRootElement annotation
> >
> > If I add the @XmlRootElement annotation to the POJO, it works fine, but
> > from
> > the documentation it seems I shouldn¹t have to do this (and I don¹t want
> > to,
> > as it is generated code).
> >
> > In reading this bug: http://issues.apache.org/jira/browse/CXF-1650
> > It appears this was an issue and should be ³fixed² in the version I am
> > using
> > (2.2 and trunk) but it¹s not clear if I am expected to configure
> > something.
> >
> > The documentation here:
> > http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-JAXBsupport states:
> > ³Alternatively to using @XmlRootElement and Collection wrappers, one can
> > provide an Object factory which will tell JAXB how to
> > marshal a given type (in case of Collections - its template type).²
> >
> > I have an ObjectFactory, generated by wsdl2java (and it looks correct),
> > but
> > I¹m not sure how I am supposed to ³provide² it other than have it on the
> > classpath (as I do).  Could you expand upon what exactly one needs to do
> > to
> > get this to work?
> >
> > Thanks,
> > Scott
> >
> > Scott Oster
> >
> > Co-Director
> > Software Research Institute
> > Center for IT Innovations in Healthcare
> >
> > Senior Researcher
> > Department of Biomedical Informatics
> >
> > The Ohio State University
> > Phone: (614) 293-9590

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog

Mime
View raw message