哪个是生成Web服务客户端的最佳maven插件?

2022-09-01 15:32:06

我必须生成一个WS客户端,我无法决定使用哪个插件。到目前为止,我的选项是:jaxb2-maven-plugin,axistools-maven-plugin和jaxws-maven-plugin。


答案 1

我必须生成一个WS客户端,我无法决定使用哪个插件。到目前为止,我的选项是:jaxb2-maven-plugin,axistools-maven-plugin和jaxws-maven-plugin。

首先,jaxb2-maven-plugin并不是真正用来生成WS客户端的。消除。

其次,就我个人而言,即使仅用于客户端开发,我也不会使用安讯士,因此我不建议使用axistools-maven-plugin。消除。

这给我们留下了JAX-WS RI和Apache CXF堆栈,以及它们各自的Maven插件:JAX-WS Maven插件(使用JAX-WS Maven插件的说明可以在“用法”页面上找到)和cxf-codegen-plugin

关于利弊,我会这样总结:

最后,两种选择都不错,所以我建议浏览一下链接并发表自己的意见。


答案 2

我使用jaxws-maven-plugin。在我看来,JAX-WS 是 WS 事实上的标准实现。它具有比AXIS更好的生成代码,并且更易于配置和实现。它有Maven和Spring支持。

从 wsdl 文件生成客户端代码,在 pom.xml中:

        <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 的 get 请求。然后,它基于驻留在自动生成的代码中的注释创建 WSDL,并对其进行比较。我相信这样做是为了确保您针对正确的服务器版本运行。因此,我将url放在我的应用程序可以访问的属性文件中,因此我在Spring应用程序上下文文件中进行初始化。

下面是使用工厂生成服务然后使用它的示例:

InternalReportsAPIService internalReportsAPIService = acmeReportsWSFactory.createInternalReportsAPIService();
InternalReportsAPI port = internalReportsAPIService.getInternalReportsAPIPort();

从这里,只需使用 port 变量调用 wsdl 上可用的任何操作。


推荐