Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing @XmlRootElement when creating a client from a wsdl

I have a question in regards to consuming a web service based on a third party wsdl file.

I've taken the given wsdl and generated the 120+ java files required. This process was done by using xjc. Within the Sping environment, I was able to successfully create a couple of JUnit tests by calling a couple of the exposed services.

But, in order to successfully test those services I had to add the @XmlRootElement annotation to the generated java files. Otherwise, I would encounter an error stating

"com.sun.istack.SAXException2: unable to marshal type "com.beam.services.client.UserGetRequestData" as an element because it is missing an @XmlRootElement annotation"

.

I've exhausted my search… I have no control as to how the wsdl file is created/structured. How can I go about generating the java files to ensure that the @XmlRootElement annotation is included or go about writing the client side code in way to avoid the error above?

Thank you.

like image 543
user310340 Avatar asked Jan 10 '12 20:01

user310340


1 Answers

If you really need the @XmlRootElement you could use simple binding mode if your type is used for only one element. The reason why JAXB does not include the annotation by default and how to use simple binding is explained here: https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always:

your schema might be used by other schemas that XJC isn't compiling right now

and

Such notion isn't defined in the spec, but as an experiment we have such aggressive optimization mode in XJC, tentatively called "simple-minded binding mode".

The sample seems to got lost when the moved the blog, but it looked like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionbindingprefixes="xjc">
  <xs:annotation>
    <xs:appinfo>
      <jaxb:globalbindings>
        <xjc:simple/>
      </jaxb:globalbindings>
    </xs:appinfo>
  </xs:annotation>      
  <xs:element name="foo" type="bar"/>
  <xs:complextype name="bar"/>
</xs:schema>

The other possibilty is to wrap it in a JAXBElement. The ObjectFactory should include a method for creating these wrapped objects.

like image 180
Drunix Avatar answered Oct 11 '22 13:10

Drunix