JAX-WS SOAP WebService using CXF

JAX-WS SOAP WebService using CXF

learninjava
Apr 26, 2015 - Webservices
 

WebService 

SOAP(Simple Object Access Protocol) is a protocol to send XML messages over HTTP. JAX-WS is the standard that defines how to create a SOAP based webservices. Let us look at a simple example on how to create a simple JAX-WS SOAP webservice using CXF and Spring. The following software has been used for this tutorial :
1. Apache Maven 3.0.4
2. Spring 3.2.6
3. Apache CXF 3.0.3
4. Jetty 6.1.10/Tomcat 7
First, we need to create a basic CXF web project structure. For this, use the below command :
mvn archetype:generate -DgroupId=com.learninjava -DartifactId=cxfjaxws
-DarchetypeArtifactId=org.apache.cxf.archetype:cxf-jaxws-javafirst -DinteractiveMode=false
 Remember!
1. If the archetype is not found due to future maven changes, you can simply use the below command :
mvn archetype:generate
Now, select the appropriate archetype serial number from the list provided. On our development environment, the serial number for our tutorial is,
515: remote -> org.apache.cxf.archetype:cxf-jaxws-javafirst (Creates a project for developing a Web service starting from Java code)
2. You can actually create a restful webservice without even writing a single line of code.
See here : JAX-WS-webservice-using-cxf-and-maven
Remember, you will just get an unpolished version of the project with out concepts being explained. So, we recommend you going through the below tutorial.
If the command is successful, your basic project structure should look similar to the below :
jaxws-webservice-project-structure
 Remember!
The maven surefire plugin expects the test file names in the following patterns :
**/Test*.java
**/*Test.java
**/*TestCase.java
There were many changes made to pom.xml that comes default with this archetype. For instance, we have removed some unused configurations and refactored the configurations for easy and better understanding.
For your convenience, we have provided configurations for running the webservice on both Jetty and Tomcat servers.
First, let us take a look at tomcat configuration. Later we will see about jetty configuration. There is a cool feature in jetty that attracts many developers. we will see what is it later in this tutorial.
 

Using Tomcat : 

1. Change the default pom.xml as below :

loading...
You will notice that there are 3 important plugins in the pom.xml out of which 1 is optional. We will see each of them in detail :
tomcat7-maven-plugin - This is the tomcat plugin where we will deploy our angry world webservice.
maven-compiler-plugin - This plugin specifies the Java compiler source and target versions.
maven-eclipse-plugin - [Optional] This plugin is used to generate the .classpath and .project files which enables us to use this project in eclipse.
The other important sections includes dependencies and properties which are used to specify the jar file dependencies and properties respectively.

2. Renamed HelloWorld.java to HelloAngryWorld.java :

loading...
This is the actual webservice source file, so lets take a look at it.
This webservice exposes only one method sayHi() which returns a String type. This method uses an important annotation:
@WebService - annotation at class level specifies that this is the webservice endpoint interface.

3. Renamed HelloWorldImpl.java to HelloAngryWorldImpl.java :

loading...

4. beans.xml :

loading...
The beans.xml has an important tag <jaxws:endpoint> which is the most important tag of this xml.
<jaxws:endpoint> - This lists all the service beans which are nothing but the services we want to make as a webservice
Observe that names of the entities like endpoint, implementor etc. These are in conjunction with SOAP protocol. You will find these names to be different when dealing with JAX-RS.

5. web.xml :

loading...
The configurations in web.xml are straighforward. CXFServlet is the actual servlet that reads the xml configurations and makes the service beans as webservices and handles the requests and responses to the exposed services.
ContextLoaderListener is the ROOT web application context that is used by spring.
Once the above files are ready, type the below command to start the tomcat container :
mvn clean package -DskipTests=true tomcat7:run
This command cleans the target directory, compiles, builds war and deploys it to tomcat. Notice that we used skipTests = true switch. This is to make sure that the test case are not run before the server starts.
Navigate to the URL :
http://localhost:9090/jaxws-service/HelloAngryWorld?wsdl
Output:

Output is the below WSDL

loading...
This is an echo service. You need a SOAP client to create a request message to test this webservice.
If you are unware of your endpoint address, you can use the below url to find out,
http://localhost:9090/jaxws-service/cxf/services
This lists all the REST and SOAP services running on your server.
 

Using Jetty : 

1. pom.xml :

The pom.xml is pretty much similar to the one we have used for tomcat configuration except that the tomcat plugin is replaced with the jetty equivalent. Also the port number is changed 8080 just to distinguish between tomcat and jetty.
loading...
All other steps are same as above.
To start the jetty container and the webservice use the below command :
mvn clean package -DskipTests=true jetty:run
Navigate to the URL :
http://localhost:8080/jaxws-service/HelloAngryWorld?wsdl
Output:

Same WSDL as above

 

Advantage of using Jetty - Hot code deployment : 

By default, Jetty scans the following directories :
target/classes - for changes to Java sources and
src/main/webapp - for changes to web sources.
If any source file is modified, the changes are autodeployed to the jetty container without manually restarting for the changes to take effect.
To test this hot code deployment, try to change any source file and recompile the source using mvn compile command.
The moment the source file is compiled, notice the server console. You will observe that jetty hot deploys the code automatically and you can access the changes via browser instantly.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by   or  or  below or on the articles on social media.
 
Like us on: