We use the WSDL file set to define API specs for SOAP Web Service. To easily and conveniently implement SOAP Web Service defined from the WSDL file, you can use the JAX-WS Maven Plugin to generate the necessary Java class files. How is it in detail? Let’s find out together in this tutorial!
First, I will create a new Maven project:
as an example.
I will define a WSDL file located in the project’s src/main/resources/wsdl directory with simple content as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://maven.huongdanjava.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldServiceService" targetNamespace="http://maven.huongdanjava.com/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://maven.huongdanjava.com/" version="1.0"> <xs:element name="hello" type="tns:hello"/> <xs:element name="helloResponse" type="tns:helloResponse"/> <xs:complexType name="hello"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="helloResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="helloResponse"> <wsdl:part element="tns:helloResponse" name="parameters"/> </wsdl:message> <wsdl:message name="hello"> <wsdl:part element="tns:hello" name="parameters"/> </wsdl:message> <wsdl:portType name="HelloWorldService"> <wsdl:operation name="hello"> <wsdl:input message="tns:hello" name="hello"/> <wsdl:output message="tns:helloResponse" name="helloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldServiceServiceSoapBinding" type="tns:HelloWorldService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="hello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="hello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="helloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldServiceService"> <wsdl:port binding="tns:HelloWorldServiceServiceSoapBinding" name="HelloWorldServicePort"> <soap:address location="http://localhost:8080/hello"/> </wsdl:port> </wsdl:service> </wsdl:definitions> |
To use the JAX-WS Maven Plugin, we will declare it in the pom.xml file as follows:
1 2 3 4 5 |
<plugin> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>4.0.2</version> </plugin> |
There are many goals defined in JAX-WS Maven Plugin such as wsimport, wsgen, … but to generate Java class files, please use wsimport goal!
You can define to use wsimport goal as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<plugin> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>4.0.2</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> <phase>install</phase> </execution> </executions> </plugin> |
With this definition, wsimport goal will be executed when we run the “mvn install” command!
To configure the hello.wsdl file that I created above to generate Java class files, the package name and folder will contain these Java class files, you can use the <configuration> tag as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<plugin> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>4.0.2</version> <configuration> <wsdlFiles> <wsdlFile>${project.basedir}/src/main/resources/wsdl/hello.wsdl</wsdlFile> </wsdlFiles> <packageName>com.huongdanjava.soap</packageName> <sourceDestDir> ${project.basedir}/src/main/java </sourceDestDir> </configuration> <executions> <execution> <goals> <goal>wsimport</goal> </goals> <phase>install</phase> </execution> </executions> </plugin> |
We use the <wsdlFiles> tag along with the list of <wsdlFile> tags to configure the path to the WSDL files we want to generate! If you want to generate all WSDL files in a directory, you can use the <wsdlDirectory> tag instead of the <wsdlFiles> tag, for example:
1 |
<wsdlDirectory>${project.basedir}/src/main/resources/wsdl</wsdlDirectory> |
Sometimes we will use a WSDL file from a link on the internet. In this case, you can use the <wsdlUrls> tag along with the list of <wsdlUrl> tags to configure the URL pointing to that path!
We configure the package name using the <packageName> tag and the directory containing the generated files using the <sourceDestDir> tag.
Now, if I run the command “mvn clean install” for my example project, you will see the following result:
JAX-WS Maven Plugin has generated the necessary files so we can implement SOAP Web Service!