[英]How do I make wsimport generate constructors?
wsimport
generates source code without parameterized constructors. wsimport
生成没有参数化构造函数的源代码。 Therefore, if the bean has many properties, one needs to invoke all the setters manually: 因此,如果bean具有许多属性,则需要手动调用所有setter:
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? 那么,有没有办法让wsimport
做这个工作? (I'm using maven wsimport plugin) (我正在使用maven wsimport插件)
To use wsimport with xjc do this: 要将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>
The critical part is the -B which will pass the -X... values on. 关键部分是-B,它将传递-X ...值。
... ...
<args>
<arg>-B-Xequals</arg>
<arg>-B-XhashCode</arg>
<arg>-B-Xvalue-constructor</arg>
</args>
... ...
This generates a value contructor, equals and hashcode methods. 这会生成一个值构造函数,equals和hashcode方法。 The equals and hashcode are provided by the jaxb2-basics plugin. 等号和哈希码由jaxb2-basics插件提供。
Use the JAXB Value Constructor Plugin for the xjc
tool. 使用xjc
工具的JAXB Value Constructor插件 。 You can use it with maven-xjc-plugin like this: 您可以将它与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>
wsimport
uses xjc
to create the Java classes. wsimport
使用xjc
创建Java类。 It supports plugins, some of which you can find at jaxb2-commons . 它支持插件,其中一些可以在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. jax-ws-commons页面提供了有关如何将XJC插件与JAX-WS Maven插件一起使用的说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.