JBoss CXF Web services

jboss cxf tutorialJBoss AS 6 ships with Apache CXF web services implementation. In this tutorial we will show how to create a simple Web service endpoint and how to deploy and test it with a client.


Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.


Developing the Web service

Create a new Dynamic Web Project from Eclipse Menu and select Target runtime and configuration “JBoss AS 6

If you don’t have a recent version of JBoss Tools installed you will not be able to see in the JBoss Community server list JBoss AS 6. However choosing JBoss AS 5 (and pointing to JBoss AS 6 installation) will just work fine

We will add at first the Web services an interface which will be used by our implementation.

package com.sample;

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

public interface Math {
 public long sum(long a, long b);

and this is the implementation class:

package com.sample;

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

@WebService(endpointInterface = "com.sample.Math", serviceName = "MathWS")

public class MathWS implements Math 

  public long sum(long a, long b) {
    System.out.println("Summing "+a+" + "+b);
    return a+b;

Now it’s time to register your Web service. Add your web service in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
 version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

A JAX-WS Endpoint can be also configured using Spring XML file in addition to using the JAX-WS APIs. Once you’ve created your server implementation, you simply need to provide the class name and an address.
Here’s a sample jbossws-cxf.xml


 <!-- one or more jaxws:endpoint POJO declarations -->
  <jaxws:endpoint id="MathWS" address="http://localhost:8080/Samplews"  
    <bean class="org.jboss.wsf.stack.cxf.InvokerJSE"/>


For more details about configuring Apache CXF web services with Apache CXF configuration file, please refer to http://community.jboss.org/wiki/JBossWS-StackCXFUserGuide

Deploy the service. If everything compiled correctly, you should see in the list of the deployed services your mathWS service.



Point the browser at http://localhost:8080/jbossws/services and check it.

jboss cxf tutorial

Develop a CXF client.

Here’s a minimal client implementation:

package com.sample;

import org.apache.cxf.interceptor.*;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {

 public static void main(String args[]) throws Exception {

  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

  factory.getInInterceptors().add(new LoggingInInterceptor());
  factory.getOutInterceptors().add(new LoggingOutInterceptor());
  Math client = (Math) factory.create();

  System.out.println("Server said: " + client.sum(3,4));



Notice the use of JaxWsProxyFactoryBean which is a factory for creating JAX-WS proxies. This class provides access to the internal properties used to set-up proxies. Using it provides more control than the standard JAX-WS APIs.