How do I make wsimport generate constructors?

送分小仙女□ 提交于 2019-12-21 09:10:19

问题


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)


回答1:


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>    



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11140317/how-do-i-make-wsimport-generate-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!