Wsimport does not create setters for collections

I use the following Ant task to create client code for the 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 clients use to transfer data to the web service, for example

 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; } } 

Note that for the bars property, the generated class does not include setter, so the client will need to access it through:

 someFoo.getBars().add("val"); 

However, the lack of a setter means that this class will not work with various tools that rely on the JavaBeans convention (e.g. Dozer), so I would like to force wsimport to generate setters. I found a thread that indicates that you can force the setter to be generated by adding the following to the Ant task

 <xjcarg value="-Xcollection-setter-injector"/> 

However, this thread is quite old, and the above seems to no longer work.

+4
source share
2 answers

You need to include the jaxb xjc extension library in your path to the assembly class and use -Xsetters xjcArg. This will force your generated stubs to have setters for your List objects.

For Maven users with 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 classpath --> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.9.5</version> </dependency> </dependencies> </plugin> 

And for Maven users using 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> 
+2
source

this functionality is provided by the plugin, the jaxb2 commons collector injector plugin. Have you included this jar in your path to xjc?

+1
source

All Articles