我必须生成一个WS客户端,我无法决定使用哪个插件.到目前为止,我的选择是:jaxb2-maven-plugin,axistools-maven-plugin和jaxws-maven-plugin.
Pas*_*ent 34
我必须生成一个WS客户端,我无法决定使用哪个插件.到目前为止,我的选择是:jaxb2-maven-plugin,axistools-maven-plugin和jaxws-maven-plugin.
首先,jaxb2-maven-plugin它并不是真正意图生成WS客户端.淘汰.
其次,我个人甚至不会使用Axis进行客户端开发,因此我不建议使用axistools-maven-plugin.淘汰.
这给我们留下了JAX-WS RI和Apache CXF堆栈,以及它们各自的Maven插件:JAX-WS Maven插件(使用JAX-WS Maven插件的说明可以在Usage页面上找到)和cxf-codegen -plugin.
关于利弊,我会这样总结一下:
最后,两个选择都不错,所以我建议稍微浏览一下这些链接,并提出自己的看法.
我使用jaxws-maven-plugin.在我看来,JAX-WS是WS的事实上的标准实现.它具有比AXIS更好的生成代码,并且更易于配置和实现.它有Maven和Spring支持.
从ps.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>
Run Code Online (Sandbox Code Playgroud)
用于创建客户端服务bean的接口(不是自动生成的):
public interface InternalReportsAPIServiceFactory {
public InternalReportsAPIService createInternalReportsAPIService();
}
Run Code Online (Sandbox Code Playgroud)
它的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个bean(用作Spring bean)的想法是有一个单例来生成客户服务代码.它需要两个输入:WSDL url - 即实现WSDL的服务器的实际URL.客户端服务代码在构造时,在提供的URL处发送WSDL的get请求.然后,它根据驻留在自动生成的代码中的注释创建WSDL,并对其进行比较.我相信这样做是为了确保您正在运行正确的服务器版本.所以,我已将url放在我的应用程序可访问的属性文件中,因此我在Spring应用程序上下文文件中初始化.
以下是使用工厂生成服务然后使用它的示例:
InternalReportsAPIService internalReportsAPIService = acmeReportsWSFactory.createInternalReportsAPIService();
InternalReportsAPI port = internalReportsAPIService.getInternalReportsAPIPort();
Run Code Online (Sandbox Code Playgroud)
从这里开始,只需使用port变量来调用wsdl上的任何可用操作.
| 归档时间: |
|
| 查看次数: |
36621 次 |
| 最近记录: |