我使用以下Ant任务为web服务端点生成客户端代码:
<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" />
生成客户端用来向web服务传递数据的类,例如
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;
}
}
请注意,对于bars
属性,生成的类不包括setter,因此客户端必须通过:
someFoo.getBars().add("val");
然而,setter的缺失意味着这个类不能与依赖于JavaBeans约定的各种工具(例如Dozer)一起工作,所以我想强制wimport生成setter。我发现了一个线程,它表明您可以通过向Ant任务添加以下内容来强制生成setter
<xjcarg value="-Xcollection-setter-injector"/>
然而,这个线程是相当旧的,上面似乎不再工作了
您需要在构建类路径中包含jaxb xjc
扩展库,并使用-Xsetters
xjcArg。这将导致生成的存根具有List
对象的设置。
对于使用jaxws-maven-plugin
的Maven用户,它看起来像这样:
<!-- 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>
对于使用cxf-codegen-plugin
的Maven用户,它看起来像这样:
<!-- 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>
这个功能是由一个插件提供的,"jaxb2 Commons collection setter injector"插件。您是否将该jar包含在XJC类路径中?