我使用以下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),所以我想强制wsimport生成setter。我发现thread表示您可以通过将以下内容添加到Ant任务来强制生成setter
<xjcarg value="-Xcollection-setter-injector"/>
但是,这个帖子已经很老了,以上似乎不再适用了。
答案 0 :(得分:2)
您需要在构建类路径中包含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>
答案 1 :(得分:1)
该功能由插件提供,即“jaxb2 commons collection setter injector”插件。你在xjc类路径中包含了那个jar吗?