wsimport doesn't generate setters for collections
I use the following Ant task开发者_如何学运维 to generate client-side code for a web service endpoint:
<wsimport wsdl="target/classes/META-INF/wsdl/MyService.wsdl"
sourcedestdir="target/wsimport"
verbose="true"
xnocompile="false"
xendorsed="true"
destdir="target/classes"
keep="true"
package="com.example.client.jaxws"
wsdlLocation="/META-INF/wsdl/MyService.wsdl" />
This generates classes that the clients use to pass data to the web service, e.g.
class Foo {
String name;
List<String> bars;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<String> getBars() {
return bars;
}
}
Notice that for the bars
property the generated class does not include a setter, so the client would have to access it via:
someFoo.getBars().add("val");
However the absence of a setter means this class won't work with various tools that rely on the JavaBeans convention (e.g. Dozer), so I would like to force wsimport to generate the setters. I found a thread that indicates you can force setter generation by adding the following to the Ant task
<xjcarg value="-Xcollection-setter-injector"/>
However, this thread is quite old, and the above doesn't seem to work anymore.
You need to include a jaxb xjc
extension library in your build classpath and use the -Xsetters
xjcArg. This will cause your generated stubs to have setters for your List
objects.
For Maven users with the jaxws-maven-plugin
, it looks like this:
<!-- generates webservice client stubs using wsimport -->
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>${jaxws-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>${basedir}/src/main/resources/${resources-folder}/wsdl/v1</wsdlDirectory>
<wsdlFile>${wsdlFileName}</wsdlFile>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/jaxb-bindings.xml</bindingFile>
</bindingFiles>
<vmArgs>
<vmArg>-Djavax.xml.accessExternalSchema=all</vmArg> <!-- necessary for JDK 8 -->
</vmArgs>
<xjcArgs>
<xjcArg>-Xsetters</xjcArg>
</xjcArgs>
</configuration>
</execution>
</executions>
<dependencies>
<!-- put xjc-plugins on the jaxws-maven-plugin's classpath -->
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.5</version>
</dependency>
</dependencies>
</plugin>
And for Maven users with the cxf-codegen-plugin
it looks like this:
<!-- generates webservice client stubs using CXF framework-->
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${adapter.api.wsdlFileName}</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/jaxb-bindings.xml</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-xjc-Xsetters</extraarg><!-- needed so that setters for lists are generated -->
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- plugin needed to customize cxf genrated classes -->
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.4</version>
</dependency>
</dependencies>
</plugin>
that functionality is provided by a plugin, the "jaxb2 commons collection setter injector" plugin. did you include that jar on your xjc classpath?
精彩评论