问题
Via using CXF 2.7.11 via Maven with the cxf-java2ws-plugin and cxf-codegen-plugin, a WSDL gets generated from a web service class and from that WSDL proxy classes are generated which then get used in various clients to connect to the actual, live server.
There is a problem with these generated service proxies in that they cause a NullPointerException when passed a null array.
The web service is something like this:
public class MyService {
public SomeResult someMethod(String param, String[] otherParams) {
if (otherParams == null){
... etc etc...
}
}
}
A generated WSDL fragment:
<xs:complexType name="someMethod">
<xs:sequence>
<xs:element minOccurs="0" name="param1" type="xs:string"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="otherParams" type="xs:string"/>
</xs:sequence>
</xs:complexType>
And finally a generated service proxy fragment :
public class SomeMethod
{
protected String param1;
protected String[] otherParams;
...
public String[] getOtherParams()
{
// Good, defensive code used here
if (this.otherParams == null)
{
return new String[0];
}
String[] retVal = new String[this.otherParams.length];
System.arraycopy(this.otherParams, 0, retVal, 0, this.otherParams.length);
return (retVal);
}
...
public void setOtherParams(String[] values)
{
// Complete lack of defensive code here!
int len = values.length;
this.otherParams = ((String[]) new String[len]);
for (int i = 0; (i < len); i++)
{
this.otherParams[i] = values[i];
}
}
public String setOtherParams(int idx,
String value)
{
// And here
return this.otherParams[idx] = value;
}
}
pom.xml fragment for WSDL generation:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-java2ws-plugin</artifactId>
<version>2.7.11</version>
<executions>
<execution>
<id>generate-wsdl-MyService</id>
<phase>process-classes</phase>
<goals>
<goal>java2ws</goal>
</goals>
<configuration>
<className>com.somewhere.services.MyService</className>
<serviceName>MyService</serviceName>
<genWsdl>true</genWsdl>
</configuration>
</execution>
</executions>
<plugin>
pom.xml fragment for proxy generation from above WSDL:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.7.11</version>
<executions>
<execution>
<id>generate-service-proxies</id>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<defaultOptions>
<bindingFiles>
<bindingFile>some/location/jaxws-binding.xjb</bindingFile>
<bindingFile>some/location/jaxb-binding.xjb</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-fe</extraarg>
<extraarg>jaxws21</extraarg>
<extraarg>-xjc-npa</extraarg>
</extraargs>
</defaultOptions>
<wsdlOptions>
<wsdlOption>
<wsdl>some/location/generated/wsdl/MyService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
</execution>
</executions>
</plugin>
How do I get CXF to generate service proxies that cope with clients wishing to pass a null array? (Not an empty array or array containing nulls, an actual null array). Do I need "otherParams" declared as "nillable=true" in the WSDL? If so, how?
I have tried many annotations and JAXB/JAXWS options and can't seem to get the desired behaviour.
来源:https://stackoverflow.com/questions/27705740/cxf-service-proxies-cause-nullpointerexception-when-passed-null-array