如何使wsimport生成构造函数?

Ale*_*lex 9 java web-services wsimport maven

wsimport生成没有参数化构造函数的源代码.因此,如果bean具有许多属性,则需要手动调用所有setter:

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

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

person.setAddress(address);
Run Code Online (Sandbox Code Playgroud)

只需编写如下代码,它就更具可读性和便捷性:

Person person = new Person("Alex", new Address("Rome"))
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法wsimport做这个工作?(我正在使用maven wsimport插件)

the*_*toy 9

要将wsimport与xjc一起使用,请执行以下操作:

    <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>
Run Code Online (Sandbox Code Playgroud)

关键部分是-B,它将传递-X ...值.

...

   <args>
      <arg>-B-Xequals</arg>
      <arg>-B-XhashCode</arg>
      <arg>-B-Xvalue-constructor</arg>
   </args>
Run Code Online (Sandbox Code Playgroud)

...

这会生成一个值构造函数,equals和hashcode方法.等号和哈希码由jaxb2-basics插件提供.

  • 知道如何从命令行执行此操作吗?就像我没有maven项目但只是想要wsimport .. (2认同)

npe*_*npe 5

使用该工具的JAXB Value Constructor插件xjc.您可以将它与maven-xjc-plugin一起使用,如下所示:

<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>    
Run Code Online (Sandbox Code Playgroud)