Apache CXF is a widely-used open-source framework for building and developing web services in Java. It provides a comprehensive infrastructure for creating SOAP-based and RESTful web services. CXF supports a variety of protocols and data formats, making it a flexible solution for web service development.
In this guide, we’ll cover the key features of Apache CXF, how it works, and how to set up web services using SOAP and REST with CXF.
1. What is Apache CXF?
Apache CXF (short for Celtix and XFire — two projects merged into one) is an open-source services framework that simplifies the development of web services in Java. It supports both SOAP (Simple Object Access Protocol) and RESTful web services, offering extensive features like security, transports, and bindings.
- SOAP Web Services: CXF provides complete support for WS- specifications*, including WS-Security, WS-ReliableMessaging, and others.
- RESTful Web Services: CXF also provides tools for creating REST APIs based on JAX-RS (Java API for RESTful Web Services).
- Data Bindings: Apache CXF integrates with different data-binding frameworks such as JAXB, XMLBeans, and Aegis.
2. Key Features of Apache CXF
- SOAP and REST Support: CXF supports both SOAP and RESTful web services, making it a flexible choice for building web applications.
- JAX-RS and JAX-WS Implementations: It supports JAX-WS for SOAP web services and JAX-RS for RESTful web services.
- Web Service Security: Apache CXF offers built-in support for WS-Security, ensuring secure web service communications.
- Extensibility: CXF is highly extensible, allowing developers to customize service and client behavior.
- Transport and Binding Support: It supports a wide variety of transports (e.g., HTTP, JMS, and more) and data bindings (e.g., JAXB, Aegis).
- WSDL Generation: Apache CXF automatically generates WSDL for SOAP services and handles SOAP message processing.
- Service Endpoint Interfaces (SEI): Supports JAX-WS SEI to expose web service endpoints.
3. Apache CXF in SOAP Web Services
CXF simplifies the development of SOAP web services by leveraging JAX-WS and WSDL. Here’s how to set up a simple SOAP service using Apache CXF.
3.1. SOAP Web Service Example
Step 1: Set up Dependencies
If you are using Maven, include the following dependencies in your pom.xml
:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.5</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.5</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>javax.jws</groupId>
<artifactId>javax.jws-api</artifactId>
<version>1.1</version>
</dependency>
Step 2: Define the Web Service Endpoint Interface (SEI)
import javax.jws.WebService;
@WebService
public interface GreetingService {
String sayHello(String name);
}
Step 3: Implement the Service
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.GreetingService")
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
Step 4: Expose the Service
Use CXF to expose the web service via a JAX-WS endpoint.
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
public static void main(String[] args) {
GreetingServiceImpl implementor = new GreetingServiceImpl();
String address = "http://localhost:8080/GreetingService";
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(GreetingService.class);
factory.setAddress(address);
factory.setServiceBean(implementor);
factory.create();
System.out.println("Service is published at: " + address);
}
}
This will start a simple SOAP service at http://localhost:8080/GreetingService
.
Step 5: Create a Client
To create a client that accesses the SOAP service, use the following code:
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
public static void main(String[] args) {
String address = "http://localhost:8080/GreetingService?wsdl";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(GreetingService.class);
factory.setAddress(address);
GreetingService client = (GreetingService) factory.create();
System.out.println(client.sayHello("World"));
}
}
This client will call the SOAP service and print the response.
4. Apache CXF in RESTful Web Services
Apache CXF also supports JAX-RS for building RESTful web services. The following steps demonstrate how to set up a simple RESTful web service using Apache CXF.
4.1. REST Web Service Example
Step 1: Add Dependencies
For Maven, include the necessary dependencies for JAX-RS support:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
Step 2: Define the REST Service
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/greeting")
public class GreetingService {
@GET
public String sayHello(@QueryParam("name") String name) {
return "Hello, " + name;
}
}
Step 3: Set up CXF to expose the RESTful service
Create a class to configure the CXF JAX-RS endpoint:
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
public class RestServer {
public static void main(String[] args) {
String address = "http://localhost:8080/rest";
GreetingService service = new GreetingService();
JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setServiceBean(service);
factory.setAddress(address);
factory.create();
System.out.println("REST service is running at: " + address);
}
}
Step 4: Create a REST Client
The following code demonstrates how to create a client to call the REST service:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
public class RestClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Response response = client.target("http://localhost:8080/rest/greeting")
.queryParam("name", "World")
.request()
.get();
System.out.println(response.readEntity(String.class));
}
}
This client will call the RESTful service and print the result.
5. Advanced Features of Apache CXF
- WS-Security: Apache CXF supports web service security (WS-Security) to ensure that your SOAP-based web services are secure.
- Message Interceptors: CXF allows you to define interceptors that can manipulate or inspect the message exchange.
- Data Bindings: CXF supports various data binding frameworks like JAXB, Aegis, and XMLBeans, allowing you to control how XML data is bound to Java objects.
- Asynchronous Services: CXF supports asynchronous processing for both SOAP and RESTful services, which helps improve scalability and responsiveness.