我使用 jaxws-maven-plugin。在我看来,JAX-WS 是 WS 的事实上的标准实现。它具有比 AXIS 更好的生成代码,并且更易于配置和实现。它支持 Maven 和 Spring。
从 pom.xml 中的 wsdl 文件生成客户端代码:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-reports-ws-code</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<!-- This property is used to support having multiple <execution> elements. The plugin has, from some reason, only one timestamp file per the all executions, thus if you have two executions, it doesn't know exactly when to recompile the code. Here we tell it explicitly to have one timestamp file per each execution --> <staleFile>${project.build.directory}/jaxws/stale/.staleFlag.reports</staleFile>
<packageName>com.acme.reports.ws.api</packageName>
<wsdlDirectory>${project.build.directory}/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>InternalReportsAPIService.wsdl</wsdlFile>
</wsdlFiles>
<verbose>true</verbose>
<sourceDestDir>${wsdl.generated.source.files.dir}</sourceDestDir>
</configuration>
</execution>
</executions>
</plugin>
创建客户端服务 bean 的接口(这不是自动生成的):
public interface InternalReportsAPIServiceFactory {
public InternalReportsAPIService createInternalReportsAPIService();
}
它的 Bean 实现:
public class InternalReportsAPIServiceFactoryBean implements InternalReportsAPIServiceFactory {
private URL acmeReportsWsdlURL;
private final static QName V1_QNAME = new QName("http://internal.reports.api.acme.net/v1","InternalReportsAPIService");
@Override
public InternalReportsAPIService createInternalReportsAPIService() {
return new InternalReportsAPIService(acmeReportsWsdlURL, V1_QNAME);
}
public void setAcmeReportsWsdlUrl(String acmeReportsWsdlUrl) {
try {
this.acmeReportsWsdlURL = new URL(acmeReportsWsdlUrl);
} catch (MalformedURLException ex) {
throw new RuntimeException("Acme Reports WSDL URL is bad: "+ex.getMessage(), ex);
}
}
}
这个bean(用作Spring bean)的想法是有一个用于生成客户端服务代码的单例。它需要两个输入: WSDL url - 即实现 WSDL 的服务器的实际 URL。客户端服务代码在构建时会在提供的 URL 处发送对 WSDL 的获取请求。然后它根据自动生成的代码中的注释创建 WSDL,并对其进行比较。我相信这样做是为了确保您运行的是正确的服务器版本。
因此,我已将 url 放置在我的应用程序可访问的属性文件中,因此我在我的 Spring 应用程序上下文文件中进行了初始化。
这是一个使用工厂生成服务然后使用它的示例:
InternalReportsAPIService internalReportsAPIService = acmeReportsWSFactory.createInternalReportsAPIService();
InternalReportsAPI port = internalReportsAPIService.getInternalReportsAPIPort();
从这里,只需使用端口变量调用 wsdl 上可用的任何操作。