1. Introduction
1.绪论
In this tutorial, we’ll show how to configure the JAX-WS maven plugin to generate Java classes from a WSDL (web service description language) file. As a result, we’ll be able to easily call web services using the generated classes.
在本教程中,我们将介绍如何配置JAX-WS maven插件,以便从WSDL(网络服务描述语言)文件中生成Java类。因此,我们将能够使用生成的类轻松调用Web服务。
2. Configuring Our Maven Plugin
2.配置我们的Maven插件
First, let’s include our JAX-WS Maven plugin with the wsimport goal in the build plugins section of our pom.xml file:
首先,让我们在pom.xml文件的构建插件部分,将我们的JAX-WS Maven插件与wsimport目标一起纳入。
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In short, the wsimport goal generates JAX-WS portable artifacts for use in JAX-WS clients and services. The tool reads a WSDL file and generates all the required artifacts for web service development, deployment, and invocation.
简而言之,wsimport目标生成用于 JAX-WS 客户端和服务的 JAX-WS 便携工件。该工具读取 WSDL 文件并生成 Web 服务开发、部署和调用所需的所有工件。
2.1. WSDL Directory Configuration
2.1.WSDL 目录配置
Within our Maven plugin section, the wsdlDirectory configuration property informs the plugin where our WSDL files are located. In this example, we’ll tell the plugin to get all WSDL files in our project’s src/main/resources directory:
在我们的Maven插件部分,wsdlDirectory配置属性告诉插件我们的WSDL文件在哪里。在本例中,我们将告诉该插件获取我们项目src/main/resources目录下的所有WSDL文件。
<plugin>
...
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
</configuration>
</plugin>
2.2. WSDL Directory-Specific Files Configuration
2.2.WSDL 目录的特定文件配置
Additionally, we can use the wsdlFiles configuration property to define a list of WSDL files to consider when generating the classes:
此外,我们可以使用wsdlFiles configuration属性来定义生成类时要考虑的WSDL文件列表。
<plugin>
...
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
<wsdlFiles>
<wsdlFile>file1.wsdl</wsdlFile>
<wsdlFile>file2.wsdl</wsdlFile>
...
</wsdlFiles>
</configuration>
</plugin>
However, when the wsdlFiles property isn’t set, all the files in the directory specified by the wsdlDirectory property will be considered.
然而,当wsdlFiles属性没有设置时,wsdlDirectory属性指定的目录中的所有文件将被考虑。
2.3. WSDL URLs Configuration
2.3.WSDL URLs 配置
Alternatively, we can configure the plugin’s wsdlUrl configuration property:
另外,我们可以配置该插件的wsdlUrl配置属性。
<plugin>
...
<configuration>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/ws/country?wsdl</wsdlUrl>
...
</wsdlUrls>
</configuration>
</plugin>
To use this option, the server hosting the URL for the WSDL file must be up and running so that our plugin can read it.
要使用这个选项,托管WSDL文件URL的服务器必须启动并运行,以便我们的插件能够读取它。
2.4. Configuring the Generated Classes Directory
2.4.配置生成的类目录
Next, in the packageName property, we can set up the generated classes package name, and in the sourceDestDir, the output directory:
接下来,在packageName属性中,我们可以设置生成的类的包名,在sourceDestDir中,设置输出目录。
<plugin>
...
<configuration>
<packageName>com.baeldung.soap.ws.client</packageName>
<sourceDestDir>
${project.build.directory}/generated-sources/
</sourceDestDir>
</configuration>
</plugin>
As a result, our final version of the plugin configuration using the wsdlDirectory option is:
因此,我们使用wsdlDirectory选项的插件配置的最终版本是。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
<packageName>com.baeldung.soap.ws.client</packageName>
<sourceDestDir>
${project.build.directory}/generated-sources/
</sourceDestDir>
</configuration>
</plugin>
3. Running the JAX-WS Plugin
3.运行JAX-WS插件
Finally, with our plugin configured, we can generate our classes with Maven and check the output logs:
最后,配置好我们的插件后,我们可以用Maven生成我们的类,并检查输出日志。
mvn clean install
[INFO] --- jaxws-maven-plugin:2.6:wsimport (default) @ jaxws ---
[INFO] Processing: file:/D:/projetos/baeldung/tutorials/maven-modules/maven-plugins/jaxws/src/main/resources/country.wsdl
[INFO] jaxws:wsimport args: [-keep, -s, 'D:\projetos\baeldung\tutorials\maven-modules\maven-plugins\jaxws\target\generated-sources', -d, 'D:\projetos\baeldung\tutorials\maven-modules\maven-plugins\jaxws\target\classes', -encoding, UTF-8, -Xnocompile, -p, com.baeldung.soap.ws.client, "file:/D:/projetos/baeldung/tutorials/maven-modules/maven-plugins/jaxws/src/main/resources/country.wsdl"]
parsing WSDL...
Generating code...
4. Check Generated Classes
4.检查生成的类
After running our plugin, we can check the output in the folder target/generated-sources configured in the sourceDestDir property.
运行我们的插件后,我们可以在target/generated-sources属性中配置的文件夹sourceDestDir中检查输出。
The generated classes can be found in com.baeldung.soap.ws.client as configured in the packageName property:
生成的类可以在com.baeldung.soap.ws.client中找到,如packageName属性所配置的。
com.baeldung.soap.ws.client.Country.java
com.baeldung.soap.ws.client.CountryService.java
com.baeldung.soap.ws.client.CountryServiceImplService.java
com.baeldung.soap.ws.client.Currency.java
com.baeldung.soap.ws.client.ObjectFactory.java
5. Conclusion
5.总结
In this article, we saw how to generate Java classes from a WSDL file using the JAX-WS plugin. As a result, we’re now able to create a web service client and use the generated classes to call our services.
在这篇文章中,我们看到了如何使用JAX-WS插件从WSDL文件中生成Java类。结果是,我们现在能够创建一个Web服务客户端,并使用生成的类来调用我们的服务。
The source code for our application is available over on GitHub.
我们的应用程序的源代码可在GitHub上获得。