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

发布于 2024-09-16 07:28:50 字数 1810 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无语# 2024-09-23 07:28:50

我必须生成一个 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 插件的说明可以在 用法 页面)和 cxf-codegen-plugin

关于优缺点,我会这样总结:

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

I have to generate a WS Client and I can't decide wich plugin to use. Until now my options are: jaxb2-maven-plugin, axistools-maven-plugin and jaxws-maven-plugin.

First, the jaxb2-maven-plugin is not really intended to generate WS clients. ELIMINATED.

Second, personally I wouldn't use Axis even for client development only so I won't recommend using the axistools-maven-plugin. ELIMINATED.

This leaves us with the JAX-WS RI and the Apache CXF stacks, and their respective Maven plugins: the JAX-WS Maven Plugin (instructions to use the JAX-WS Maven Plugin can be found on the Usage page) and the cxf-codegen-plugin.

Regarding the pros and cons, I would summarize them like this:

At the end, both choices are decent so I suggest to browse the links a bit and to make your own opinion.

绾颜 2024-09-23 07:28:50

我使用 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 上可用的任何操作。

I use jaxws-maven-plugin. In my opinion, JAX-WS is the de-facto standard implementation for WS. It has much better generated code than AXIS, and easier to config and implement. It has Maven and Spring support.

Generating client-side code from wsdl file, in 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>

An interface to create the client service bean (this is not auto generated):

public interface InternalReportsAPIServiceFactory {

    public InternalReportsAPIService createInternalReportsAPIService();

}

Its Bean implementation:

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);
        }
    }
}

The idea in this bean (used as Spring bean) is to have a singleton for generating a client service code. It requires two inputs: The WSDL url - that is, the actual URL of the server which implements the WSDL. The client service code, upon construction, send a get request for the WSDL at the supplied URL. It then creates the WSDL based on the annotations residing in the auto generated code, and it compares it. I believe this is done to make sure you're running against the correct server version.
So, I've placed the url in a property file accessible to my application, thus I initialize in my Spring application context file.

Here's an example of using the factory to generate a service and then using it:

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

From here, just use the port variable to call any operation available on the wsdl.

浅唱ヾ落雨殇 2024-09-23 07:28:50

所需的 Maven 插件:

  • cxf-java2ws-plugin(JAX-WS 到 WSDL)
  • cxf-codegen-plugin(WSDL 到 Java)

JAX-WS 到 WSDL
通过使用“java2ws”目标配置 cxf-java2ws-plugin,从 JAX-WS 带注释的类生成 WSDL 文档。

添加 JAX-WS 带注释的类所需的 cxf-rt-frontend-jaxws 依赖项和项目依赖项作为插件依赖项。

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-java2ws-plugin</artifactId>
   <version>2.5.1</version>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>2.5.1</version>
      </dependency>
      <dependency>
         <groupId>com.medici.app</groupId>
         <artifactId>services</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <className>com.medici.app.services.WebServiceBean</className>
            <genWsdl>true</genWsdl>
         </configuration>
         <goals>
            <goal>java2ws</goal>
         </goals>
      </execution>
   </executions>
</plugin>

WSDL 2 Java
通过使用“wsdl2java”目标配置 cxf-codegen-plugin,从 WSDL 文档生成 Java 客户端。

'-p' 参数指定包类。

生成的类将放置在 target/ generated-sources/cxf 文件夹中。

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
      <execution>
         <id>process-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${project.build.directory}/wsdl/WebService.wsdl</wsdl>
                  <extraargs>
                     <extraarg>-p</extraarg>
                     <extraarg>com.medici.app.client.model</extraarg>
                  </extraargs>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>

Maven plugins required:

  • cxf-java2ws-plugin (JAX-WS to WSDL)
  • cxf-codegen-plugin (WSDL to Java)

JAX-WS to WSDL
To generate the WSDL document from the JAX-WS annotated class by configuring cxf-java2ws-plugin with the ‘java2ws’ goal.

Add the cxf-rt-frontend-jaxws dependency and project dependencies required for the JAX-WS annotated class as plugin dependencies.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-java2ws-plugin</artifactId>
   <version>2.5.1</version>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>2.5.1</version>
      </dependency>
      <dependency>
         <groupId>com.medici.app</groupId>
         <artifactId>services</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <className>com.medici.app.services.WebServiceBean</className>
            <genWsdl>true</genWsdl>
         </configuration>
         <goals>
            <goal>java2ws</goal>
         </goals>
      </execution>
   </executions>
</plugin>

WSDL 2 Java
To generate the Java client from the WSDL document by configuring cxf-codegen-plugin with ‘wsdl2java’ goal.

The ‘-p’ argument specifies the package classes .

The generated classes will be placed in the target/generated-sources/cxf folder.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
      <execution>
         <id>process-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${project.build.directory}/wsdl/WebService.wsdl</wsdl>
                  <extraargs>
                     <extraarg>-p</extraarg>
                     <extraarg>com.medici.app.client.model</extraarg>
                  </extraargs>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文