I have just updated JAXBElementProvider to properly wrap into JAXBElements those JAXB-generated types which are missing for whatever reasons @XmlRootElement annotations.
'jaxbElementClassNames' list property I referred to earlier can only be used with classes which do have @XmlRootElement annotations, it is really only useful if the serialization of the derived types needs to be affected. I've added a similar jaxbElementClassMap property which contains className to element name pairs, with element names being either simple names or expanded qnames. If an object class has no @XmlRootElement but its name is a key in this map then JAXBElement will be created internally and serialized properly. I think it can be quite useful in a number of cases. Scott - if you need this feature right now then let me know please and I can help you to create a custom JAXBElementProvider which will do it for you, till the fix makes it into the next 2.2.x release. thanks, Sergey Sergey Beryozkin-2 wrote: > > Hi > > Returning JAXBElement<Book> will work in JAX-RS as it is required by the > spec, provided it's possible for a user to have explicit JAXBElements in > method signatures (for ex, return types). > > JAX-RS JAXBElementProvider can actually be told to wrap a given object in > a JAXBElement, but so far this option (setting a 'jaxbElementClassNames' > list property containing class names) has only been used to affect the > serialization of derived types, for an xsi:type attribute be added. I > tried that option to wrap the objects of types with no @XmlRootElement > hoping it would work but unfortunately it did not, still @XmlRootElement > was needed. I'll give it another try though... > >> I suppose we could allow @XmlElement on the method for the return, but >> until we go JAXB 2.2 ... > > Interesting, it will be definitely worth investigating > > cheers, Sergey > > > > -----Original Message----- > From: Daniel Kulp [mailto:[email protected]] > Sent: Wed 8/5/2009 11:42 AM > To: [email protected] > Cc: Sergey Beryozkin > Subject: Re: @XmlRootElement with jax-rs/jax-ws > > > 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 > [email protected] > http://www.dankulp.com/blog > > > -- View this message in context: http://www.nabble.com/%40XmlRootElement-with-jax-rs-jax-ws-tp24795823p24864049.html Sent from the cxf-user mailing list archive at Nabble.com.
