问题
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