4

I have one set of Java classes generated from a WSDL that work fine; I'm adding another WSDL to the project for another webservice I'm using, but I get no @XmlRootElement annotations in the generated classes from the second WSDL, and don't understand why not.

Here's the plugin portion of the pom:

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>jaxb2-maven-plugin</artifactId>
          <version>2.2</version>
          <executions>
              <execution>
                  <id>xjc</id>
                  <goals>
                      <goal>xjc</goal>
                  </goals>
              </execution>
          </executions>
          <configuration>
              <sourceType>wsdl</sourceType>
              <sources>
                  <source>${resources.path}my-module/src/main/resources/wsdl/w1.wsdl</source>
                  <source>${resources.path}my-module/src/main/resources/wsdl/w2.wsdl</source>
              </sources>
              <extension>true</extension>
              <xjbSources>
                  <xjbSource>src/main/resources/xjb/bindings.xjb</xjbSource>
              </xjbSources>
          </configuration>
        </plugin>

And here's bindings.xjb:

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings>
        <!-- annotate tag here? -->
        <jxb:globalBindings>
            <xjc:simple/>
        </jxb:globalBindings>
    </jxb:bindings>
</jxb:bindings>

I read in this SO post about using an annotate tag, so I inserted

        <annox:annotate>
            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" />
        </annox:annotate>

at the indicated place in the bindings.xjb file, but of course I don't have a definition for the annox prefix, so that doesn't work. The post doesn't indicate where that's defined.

I've also been through multiple answers in this SO post; unfortunately the various methods each leave out some steps. For instance, I'm willing to call marshall and unmarshall methods directly, but I need to know where to get the "JAXBContext" they speak of, and what the unmarshall call looks like, or where to look it up.

Is annox the correct way to do this? Is there another correct way to do this?

Martin
  • 2,573
  • 28
  • 22
arcy
  • 12,845
  • 12
  • 58
  • 103
  • Can you post w1/w2.wsdl (maybe in simplified form)? – Scott Kurz Mar 04 '17 at 11:34
  • @ScottKurz Will be happy to, but it will have to wait until Monday (Eastern US Time); just tried to get into my work computer and there's something wrong with the login sequence... – arcy Mar 04 '17 at 15:19
  • the simplest way for jaxb2-maven-plugin is here https://stackoverflow.com/a/10613686 – Denis Orlov Dec 02 '21 at 10:56

2 Answers2

3

I just got done converting to from the old jaxb2-maven-plugin that is a dependency of only 1 other project in Maven to maven-jaxb2-plugin which is a dependency for 18. There seems to be much more documentation for it. Check out the JAXB2 Maven Plugin Wiki

Here is a pom.xml example:

<build>
<plugins>
  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.13.2</version>
    <executions>
      <execution>
        <id>generate</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <schemaDirectory>src/main/resources/</schemaDirectory>
      <generateDirectory>${project.build.directory}/generated-sources/jaxb</generateDirectory>
      <schemaIncludes>
        <include>MyXSD.xsd</include>
      </schemaIncludes>
      <schemaExcludes>
        <include>ObeXSD.xsd</include>
      </schemaExcludes>
      <args>
        <arg>-Xannotate</arg>
      </args>
      <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>1.0.2</version>
        </plugin>
      </plugins>
    </configuration>
  </plugin>
  <plugin>
      <inherited>true</inherited>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
          <source>1.7</source>
          <target>1.7</target>
      </configuration>
  </plugin>

Here is an example xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.1" 
    xmlns:annox="http://annox.dev.java.net" 
    jaxb:extensionBindingPrefixes="annox">
    <xsd:complexType name="AbstractProblemClass" abstract="true">
    <xsd:sequence />
    </xsd:complexType>

    <xsd:complexType name="ConcreteClass">
    <xsd:annotation>
        <xsd:appinfo>
            <annox:annotate target="class">@javax.xml.bind.annotation.XmlRootElement</annox:annotate>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:complexContent>
        <xsd:extension base="AbstractProblemClass">
            <xsd:sequence>
                <xsd:element name="Stuff" type="xsd:String" />
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>
1

For jakarta based annotations (Spring Boot 3 uses them eg) this one worked for me:

<dependencies>
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>${jaxb2-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <verbose>true</verbose>
                        <packageName>package.name</packageName>
                        <sourceType>wsdl</sourceType>
                        <xjbSources>
                            <xjbSource>src/main/resources/bindings.xjb</xjbSource>
                        </xjbSources>
                        <sources>
                            <source>src/main/resources/wsdl/some.wsdl</source>
                        </sources>
                        <outputDirectory>target/generated-sources/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

bindings.xjb file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jxb:bindings version="3.0" xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc">
    <jxb:globalBindings>
        <xjc:simple/> <!-- adds @XmlRootElement annotations -->
    </jxb:globalBindings> </jxb:bindings>
Rasto
  • 11
  • 1