Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make wsimport generate constructors?

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)

like image 822
Alex Avatar asked Jun 21 '12 14:06

Alex


2 Answers

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.

like image 82
theINtoy Avatar answered Oct 01 '22 03:10

theINtoy


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>    
like image 42
npe Avatar answered Oct 01 '22 02:10

npe