通过 cxf-java2ws-plugin 和 cxf-codegen-plugin 通过 Maven 使用 CXF 2.7.11,从 Web 服务类生成 WSDL,然后生成 WSDL 代理类,然后在各种客户端中使用以连接到实际的实时服务器。
这些生成的服务代理存在问题,因为它们在传递空数组时会导致 NullPointerException。
Web 服务是这样的:
public class MyService {
public SomeResult someMethod(String param, String[] otherParams) {
if (otherParams == null){
... etc etc...
}
}
}
生成的 WSDL 片段:
<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>
最后是生成的服务代理片段:
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;
}
}
用于 WSDL 生成的 pom.xml 片段:
<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>
从上面的 WSDL 生成代理的 pom.xml 片段:
<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>
如何让 CXF 生成服务代理来处理希望传递空数组的客户?(不是空数组或包含空值的数组,而是实际的空数组)。我需要在 WSDL 中声明为“nillable=true”的“otherParams”吗?如果是这样,怎么做?
我尝试了许多注释和 JAXB/JAXWS 选项,但似乎无法获得所需的行为。