How do I make wsimport generate constructors?
Asked Answered
A

3

11

wsimport generates source code without parameterized constructors. Therefore, if the bean has many properties, one needs to invoke all the setters manually:

Person person = new Person();
person.setName("Alex");

Address address = new Address();
address.setCity("Rome");

person.setAddress(address);

It's much more readable and convenient to just write the code like this:

Person person = new Person("Alex", new Address("Rome"))

So, is there any way to make wsimport do this job? (I'm using maven wsimport plugin)

Amagasaki answered 21/6, 2012 at 14:23 Comment(0)
S
5

Use the JAXB Value Constructor Plugin for the xjc tool. You can use it with maven-xjc-plugin like this:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xjc-maven-plugin</artifactId>
        <version>1.0-beta-2-SNAPSHOT</version>
        <executions>
          <execution>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <task><![CDATA[
                <xjc schema="src/main/resources/com/acme/services.xsd" package="com.acme">
                   <arg value="-Xvalue-constructor" />
                </xjc>
              ]]></task>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>    
Strainer answered 21/6, 2012 at 14:38 Comment(2)
Can you tell me where I can find repository with this artifact? Thanks.Amagasaki
It's in the maven cetralStrainer
T
11

To use wsimport with xjc do this:

    <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.3</version>

            <dependencies>
                <!-- put xjc-plugins on the jaxws-maven-plugin's classpath -->
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.4</version>
                </dependency>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-value-constructor</artifactId>
                    <version>3.0</version>
                </dependency>
            </dependencies>
            <executions>
                          <execution>
                    <id>wsdl-gen</id>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <wsdlDirectory>${project.basedir}/src/main/resources/wsdl/</wsdlDirectory>
                        <bindingDirectory>${project.basedir}/src/main/resources/wsdl</bindingDirectory>
                        <sourceDestDir>${project.build.directory}/generated-sources/wsimport</sourceDestDir>
                        <extension>true</extension>
                        <target>2.2</target>
                        <verbose>true</verbose>
                        <!-- tell JAXB to actually use xjc-plugins -->
                        <args>
                            <arg>-B-Xequals</arg>
                            <arg>-B-XhashCode</arg>
                            <arg>-B-Xvalue-constructor</arg>
                        </args>
                    </configuration>
                </execution>
    </executions>
        </plugin>

The critical part is the -B which will pass the -X... values on.

...

   <args>
      <arg>-B-Xequals</arg>
      <arg>-B-XhashCode</arg>
      <arg>-B-Xvalue-constructor</arg>
   </args>

...

This generates a value contructor, equals and hashcode methods. The equals and hashcode are provided by the jaxb2-basics plugin.

Twicetold answered 2/1, 2014 at 10:20 Comment(1)
Any idea how to do this from the command line? Like I don't have a maven project but just want to wsimport..Moran
S
5

Use the JAXB Value Constructor Plugin for the xjc tool. You can use it with maven-xjc-plugin like this:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xjc-maven-plugin</artifactId>
        <version>1.0-beta-2-SNAPSHOT</version>
        <executions>
          <execution>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <task><![CDATA[
                <xjc schema="src/main/resources/com/acme/services.xsd" package="com.acme">
                   <arg value="-Xvalue-constructor" />
                </xjc>
              ]]></task>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>    
Strainer answered 21/6, 2012 at 14:38 Comment(2)
Can you tell me where I can find repository with this artifact? Thanks.Amagasaki
It's in the maven cetralStrainer
W
0

wsimport uses xjc to create the Java classes. It supports plugins, some of which you can find at jaxb2-commons. There is also a constructor plugin, which creates a constructor with parameters for all child elements.

The jax-ws-commons page has instructions on how to use XJC plugins with the JAX-WS Maven plugin.

Waers answered 21/6, 2012 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.