Java 哪个是生成 Web 服务客户端的最佳 Maven 插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3587982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is the best maven's plugin to generate a Web Service Client?
提问by Neuquino
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.
我必须生成一个 WS 客户端,但我无法决定使用哪个插件。到目前为止,我的选择是:jaxb2-maven-plugin、axistools-maven-plugin 和 jaxws-maven-plugin。
采纳答案by Pascal Thivent
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.
我必须生成一个 WS 客户端,但我无法决定使用哪个插件。到目前为止,我的选择是:jaxb2-maven-plugin、axistools-maven-plugin 和 jaxws-maven-plugin。
First, the jaxb2-maven-plugin
is not really intended to generate WS clients. ELIMINATED.
首先,jaxb2-maven-plugin
它并不是真正打算生成 WS 客户端。消除。
Second, personally I wouldn't use Axis even for client development onlyso I won't recommend using the axistools-maven-plugin
. ELIMINATED.
其次,就我个人而言,即使仅用于客户端开发,我也不会使用 Axis,因此我不建议使用axistools-maven-plugin
. 消除。
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 Usagepage) and the cxf-codegen-plugin.
这给我们留下了 JAX-WS RI 和 Apache CXF 堆栈,以及它们各自的 Maven 插件:JAX-WS Maven 插件(可以在使用页面上找到使用 JAX-WS Maven 插件的说明)和cxf-codegen -插件。
Regarding the pros and cons, I would summarize them like this:
关于利弊,我总结如下:
- JAX-WS RI is included in Java 6, but the documentation is more "rough" (although you'll find plenty of tutorials about JAX-WS RI too).
- Apache CXF is better documentated and provide more flexibility if you want to go beyond the spec.
- JAX-WS RI 包含在 Java 6 中,但文档更加“粗糙”(尽管您也会找到大量关于 JAX-WS RI 的教程)。
- 如果您想超越规范,Apache CXF 有更好的文档记录并提供更大的灵活性。
At the end, both choices are decent so I suggest to browse the links a bit and to make your own opinion.
最后,这两种选择都不错,所以我建议稍微浏览一下链接并发表自己的意见。
回答by Asaf Mesika
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.
我使用 jaxws-maven-plugin。在我看来,JAX-WS 是事实上的 WS 标准实现。它生成的代码比 AXIS 好得多,而且更容易配置和实现。它有 Maven 和 Spring 支持。
Generating client-side code from wsdl file, in pom.xml:
从 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>
An interface to create the client service bean (this is not auto generated):
创建客户端服务 bean 的接口(这不是自动生成的):
public interface InternalReportsAPIServiceFactory {
public InternalReportsAPIService createInternalReportsAPIService();
}
Its Bean implementation:
它的 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);
}
}
}
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.
这个 bean(用作 Spring bean)的想法是有一个单例来生成客户端服务代码。它需要两个输入: WSDL url - 即实现 WSDL 的服务器的实际 URL。客户端服务代码在构造时发送对提供的 URL 处的 WSDL 的获取请求。然后,它根据驻留在自动生成的代码中的注释创建 WSDL,并对其进行比较。我相信这样做是为了确保您运行的是正确的服务器版本。因此,我已将 url 放在我的应用程序可访问的属性文件中,因此我在我的 Spring 应用程序上下文文件中进行了初始化。
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.
从这里开始,只需使用 port 变量来调用 wsdl 上可用的任何操作。
回答by Tiago Medici
Maven plugins required:
需要 Maven 插件:
- cxf-java2ws-plugin (JAX-WS to WSDL)
- cxf-codegen-plugin (WSDL to Java)
- cxf-java2ws-plugin(JAX-WS 到 WSDL)
- cxf-codegen-plugin(WSDL 到 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.
JAX-WS 到 WSDL 通过使用“java2ws”目标配置 cxf-java2ws-plugin,从 JAX-WS 注释类生成 WSDL 文档。
Add the cxf-rt-frontend-jaxws dependency and project dependencies required for the JAX-WS annotated class as plugin dependencies.
将 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 To generate the Java client from the WSDL document by configuring cxf-codegen-plugin with ‘wsdl2java' goal.
WSDL 2 Java 通过使用“wsdl2java”目标配置 cxf-codegen-plugin,从 WSDL 文档生成 Java 客户端。
The ‘-p' argument specifies the package classes .
'-p' 参数指定包类。
The generated classes will be placed in the target/generated-sources/cxf folder.
生成的类将放置在 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>