cxf-xjc-plugin: how to add @XMLRoot Annotation to complex type?

501 views Asked by At

I have a XSD Schema and there is an "any" element

<xs:any minOccurs="0"
                 maxOccurs="unbounded"
                 namespace="http://the-namespace.org"
                 processContents="lax"/>

That means that here I can add any element defined in "http://the-namespace.org" and that is exactly what im Trying to do:

    return MyObject.builder()
        // ...
        .withAny(AnyObjectFromNamespace.builder().build())
        .build();

When I now run the xsdtojava I get an error:

Caused by: com.sun.istack.SAXException2: Marshalling von Typ "AnyObjectFromNamespace" as element is not possible, because a @XmlRootElement-Annotation is missing

So my question is now how I can add the XmlRootElement annotation without changing my xsd schema?`

This is my pom.xml:

  <dependencies>
    <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics</artifactId>
      <version>1.11.1</version>
    </dependency>

    <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics-runtime</artifactId>
      <version>1.11.1</version>
    </dependency>

    <!-- needed for builder plugin -->
    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.3.3</version>
    </dependency>

    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-core</artifactId>
      <version>2.3.0.1</version>
    </dependency>

    <!-- java time instead of xml gregorian calendar -->
    <dependency>
      <groupId>com.migesok</groupId>
      <artifactId>jaxb-java-time-adapters</artifactId>
      <version>1.1.3</version>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <extensions>
            <!-- Default Values -->
            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:3.3.0</extension>
            <!-- Equals, HashCode, ToString, etc. -->
            <extension>org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1</extension>
            <!-- Builder, Immutable, etc. -->
            <extension>net.codesup.util:jaxb2-rich-contract-plugin:2.0.1</extension>
          </extensions>
        </configuration>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>xsdtojava</goal>
            </goals>
            <configuration>
              <sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
              <xsdOptions>
                <xsdOption>
                  <extension>true</extension>
                  <xsd>${basedir}/src/main/resources/xsd/schema.xsd</xsd>
                  <bindingFile>${basedir}/src/main/resources/xjb/binding.xjb</bindingFile>
                  <extensionArgs>
                    <!-- Enable Default Values Generation -->
                    <extensionArg>-Xdv</extensionArg>
                    <!-- Enable Equals Generation -->
                    <extensionArg>-Xequals</extensionArg>
                    <!-- Enable Hashcode Generation -->
                    <extensionArg>-XhashCode</extensionArg>
                    <!-- Enable ToString Generation -->
                    <extensionArg>-XtoString</extensionArg>
                    <!-- Enable Builder Generation -->
                    <extensionArg>-Xfluent-builder</extensionArg>
                  </extensionArgs>
                </xsdOption>
              </xsdOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
0

There are 0 answers