Apache CXF – JAX-WS – Simple Tutorial

A lot of Java Developers out there sees task with Web Service implementation daunting – well, nobody can blame them really, especially that it brings a lot of  complexity in development and design over the years of enterprise application development. For some though, learning it is somehow the next step in building a full blown enterprise application – Web Services – is one of the key schemes for implementing service oriented design – almost every platform available has support to the scheme, and that of course includes Java/J2EE.

In this tutorial, I will show how you will using Apache CXF to create  Web Servies using JAX-WS and the Apache CXF API to call specific service from a remote application.
 

  1. Building the Web Service Application : Server Side  – We need to build the application as well as the Web Services implementation code. This will be a generic WAR (Web Archive) that will house the actual implementation
  2. Building the Web Service Application: Client Side – Of course, we need to provide external clients a way for them to access the services – we will create a stub and give them out to clients for them to use the services.

FYI: I’m using Maven to simplify library definitions.

1st we create the Stub

this houses the interface and entities (Pojos) used to access the service. When you design your service – you usually create the class interfaces (reference) first and put it on another JAR file for re-distribution.

2nd lets build the service.

We will be hosting the service from a Web Archive deployed to an application server.

POM. xml – We will be using Spring CDI and CXF – Might as well load up all the libraries needed.

We include the stub on the server as this will act as the reference of our implementation:

We now create the Service Implementation:

cxf-servlet (or beans.xml) – this is where we will call the factory that will publish the WSDL upon loading the context.

web.xml – we need to make sure that web service request will go thru CXF – since we are using its framework. Good thing about CXF is it does all the necessary binding, marshalling, unmarshalling as well as injecting the in / out interceptors.

Build and Deploy the WAR! – After deploying the war, view the context page. You should be seeing something like this:

If you manage to view the above URL – then you have successfully expose your service and can now be called by external clients

3rd we create the client app.

Now we create a standalone Java app to call the service – we will be using Spring again to context load client beans and we need include the stub so that we can have reference to the implementation.

beans.xml – create the xml and call the proxy factory bean to create the instance on runtime

Create the Client – We will now call the bean and use the reference to call the method.

4th Test! – Test your service!

You should now be able call the Service implementation!

Download the example here

With this simple example, its really a conclusive proof that creating Web Service now is just a breeze – We took advantage of Apache CXF as a service framework to simplify the development and Springs powerful CDI that almost took care the rest.

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

3 Responses to "Apache CXF – JAX-WS – Simple Tutorial"

  1. Ben Wilcock says:

    It is simple. But developers should be aware that it’s not always ideal, because in samples like this one the Java code determines the Web Service’s WSDL contract (often called a contract-last approach) which can introduce coupling issues for service consumers.

    In SOA, contract-first (where the code is shaped by the WSDL no the other way round) is considered the better approach to creating enterprise web services but it is more complicated. However, JAX-WS and CXF both support this method, and its the only method supported by Spring. See http://static.springsource.org/spring-ws/sites/2.0/reference/html/why-contract-first.html for more.

    Hope this helps.

  2. Praveen D says:

    superb…

  3. Thai says:

    Why don’t I see the cxf.xml, cxf-extension-http.xml and cxf-extension-soap.xml files in the downloadable zip?

Leave a Reply


4 + = six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close