webservices apache cxf tutorial jbossIn this tutorial we will show how you can easily to install Apache-CXF on the new JBoss AS 7 application server.

Why Do we need to installing Apache CXF on JBoss AS ? JBoss AS 6/7 by default ships with Apache CXF's JAX-WS implementation,
so if you need to develop regular JAX-WS Web services you have already all you need. However if you need specific Apache CXF functionalities
you need to install them on the application server.

So Let's see how to install Apache CXF on your JBoss AS.

Apache CXF downloads are available at: http://www.jboss.org/jbossws/downloads
At the time of writing the latest release is jbossws-cxf-4.0.0.CR1

Unpack the distribution and, at the root of it create a new file ant.properties starting from the ant.properties.example

copy ant.properties.example ant.properties

Now customize this file for your server release, for example, if you are going to installa Apache CXF on JBoss AS 7
you have to customize these rows:


Now issue the following ant task:

ant -Dspring=true deploy-jboss702

This will install both Apache CXF libraries and Spring support as modules in JBoss AS 7.

Now restart JBoss AS.
If you are using JBoss AS 7.0.0 - 7.0.2 then you need to use the standalone-preview.xml configuration:
standalone.bat --server-config=standalone-preview.xml
If you are using JBoss AS 7.1.0, then the Web service domain is available as the default configuration:

Ok. So If everything was successfully installed, you should see the following message when booting the server:

19:12:12,977 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-2) JBoss Web Services - Stack CXF Server 4.0.0.CR1

Now let's test a sample Web service which uses native Apache CXF functionalities. Create a new Web project from your favourite IDE:

package com.sample.ws;

import javax.jws.WebService;
import org.apache.cxf.interceptor.*;

@WebService(endpointInterface = "com.sample.ws.SampleWS", serviceName = "SampleWS")
@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")
@OutInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingOutInterceptor")

public class SampleWSImpl implements SampleWS {
 public String hello(String s) {
 return "Hello "+s;

And this is the Service Interface:
package com.sample.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;


public interface SampleWS {
 public String hello(String s);

You will need to declare this Web service as Servlet in web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"  version="3.0">


This Web service uses a native Apache CXF functionality called Interceptor.
Interceptors are the fundamental processing unit inside CXF. When a service is invoked, an InterceptorChain is created and invoked. Each interceptor gets a chance to do what they want with the message. This can include reading it, transforming it, processing headers, validating the message, etc.
This kind of Interceptor will print the SOAP envelope which is received by the server and the one which is returned to the client.

Last thing we need to add are Spring and JAX-WS libraries. Since we have just installed them on the application server, all you need is adding a Dependency to these modules into the META-INF/MANIFEST.MF file:

Manifest-Version: 1.0
Dependencies: org.apache.cxf org.springframework.spring

Now your application is completed and when packaged, it should look like this:

?         MANIFEST.MF (With Dependencies in it)
?   web.xml
?        ?
?        ????com
?                ????sample
?                       ????ws
?                                   SampleWS.class
?                                   SampleWSImpl.class

Deploy your application and verify from the logs that it was correctly deployed.

Now let’s build a regular JAX-WS client for our Web service:
package com.sample.ws.client;

import java.net.URL;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

public class TestWS {

public static void main(String[] args) throws Exception {
   URL wsdlURL = new URL("http://localhost:8080/ExampleWS/ws?wsdl");  
   QName SERVICE_NAME = new QName("http://ws.sample.com/", "SampleWS");
   Service service = Service.create(wsdlURL, SERVICE_NAME);
   SampleWS client = service.getPort(SampleWS.class);
   String s = client.hello("Frank");


If everything was correctly configured, the application will contact the server, returning a message and logging the in/out SOAP messages on the server:

{xtypo_code}ID: 1
Address: http://localhost:8080/ExampleWS/ws
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Authorization=[], cache-control=[n
o-cache], connection=[keep-alive], Content-Length=[184], content-type=[text/xml;
charset=UTF-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user
-agent=[Apache CXF 2.4.4]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:hello xmlns:ns2="http://ws.sample.com/"><arg0>Frank</arg0></ns2:hello></soap:Body></soap:Envelope>
19:24:40,510 INFO  [org.apache.cxf.interceptor.LoggingOutInterceptor] (http- Outbound Message
ID: 2
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><
soap:Body><ns2:helloResponse xmlns:ns2="http://ws.sample.com/"><return>Hello Frank</return></ns2:helloResponse></soap:Body></soap:Envelope>
19:24:40,519 INFO  [stdout] (http- Hello Frank{/xtypo_code}

In the next tutorial we will have a closer look at the Interceptors functionalities of Apache CXF to achieve some advanced functionalities like Authentication.



Related articles available on mastertheboss.com

JBoss web services

JAX-WS simplifies the development model for a web service endpoin

What is a Web Service One Way invocation?

JBoss recipe of the day

Asynchronous web services with JBoss WS

Developing rigorous and responsive web service client application

How to change the default Web Service deployment Port ?

JBoss recipe of the day

Using Axis Web Services with JBoss

Still not ready for JBoss WS ? if you don't have a JDK 1.5 compli

Invoking JBoss Web Services with Flex

Flex® is a free, open source framework for building highly in