wsimport在没有参数化构造函数的情况下生成源代码。因此,如果bean有许多属性,则需要手动调用所有setter:
Person person = new Person();
person.setName("Alex");
Address address = new Address();
address.setCity("Rome");
person.setAddress(address);只需像这样编写代码会更具可读性,也更方便:
Person person = new Person("Alex", new Address("Rome"))那么,有没有办法让wsimport来做这项工作呢?(我使用的是maven wsimport插件)
发布于 2012-06-21 22:38:25
使用xjc工具的JAXB Value Constructor Plugin。您可以将其与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> 发布于 2014-01-02 18:20:27
要将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>关键部分是-B,它将通过-X...值打开。
..。
<args>
<arg>-B-Xequals</arg>
<arg>-B-XhashCode</arg>
<arg>-B-Xvalue-constructor</arg>
</args>..。
这会生成一个值构造器、equals和hashcode方法。equals和hashcode由jaxb2-basics插件提供。
发布于 2012-06-21 22:40:15
wsimport使用xjc来创建Java类。它支持插件,其中一些你可以在jaxb2-commons上找到。还有一个构造器插件,它为所有子元素创建一个带有参数的构造器。
jax-ws-commons页面上有关于如何将XJC插件与JAX-WS Maven插件一起使用的说明。
https://stackoverflow.com/questions/11140317
复制相似问题