Apache CXF

Language: Java

Web

Apache CXF was developed to simplify building and deploying web services in Java. It offers a flexible architecture supporting multiple protocols (HTTP, JMS, TCP), pluggable data bindings (JAXB, JSON, XML), and integration with Spring and CDI. CXF is widely used in enterprise applications that require interoperability, SOAP-based services, or hybrid REST/SOAP architectures.

Apache CXF is a framework for building robust web services in Java. It supports both RESTful (JAX-RS) and SOAP-based (JAX-WS) services, providing tools for service creation, client generation, and integration with various transports and data bindings.

Installation

maven: Add dependencies in pom.xml: <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.6.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.6.1</version> </dependency>
gradle: Add dependencies in build.gradle: implementation 'org.apache.cxf:cxf-rt-frontend-jaxrs:3.6.1' implementation 'org.apache.cxf:cxf-rt-transports-http:3.6.1'

Usage

CXF allows developers to create REST or SOAP services using annotations, XML configuration, or Spring integration. It supports automatic WSDL generation for SOAP, JSON/XML marshalling for REST, exception handling, interceptors, and client APIs for consuming services.

Simple JAX-RS REST service

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class HelloService {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello Apache CXF!";
    }
}

Defines a basic GET endpoint at `/hello` that returns plain text.

Creating a SOAP endpoint

import jakarta.jws.WebService;
@WebService(endpointInterface = "com.example.HelloService")
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

Defines a simple SOAP web service using JAX-WS annotations.

Publishing REST service programmatically

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setResourceClasses(HelloService.class);
factory.setAddress("http://localhost:8080/");
factory.create();

Shows how to publish a REST service programmatically using CXF.

SOAP client example

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloService.class);
factory.setAddress("http://localhost:8080/HelloService");
HelloService client = (HelloService) factory.create();
String response = client.sayHello("Alice");

Creates a SOAP client for a CXF service using JaxWsProxyFactoryBean.

Using CXF interceptors

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());

Adds logging interceptors to log inbound and outbound messages for debugging.

Exception handling in REST

import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.core.Response;

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Exception> {
    public Response toResponse(Exception e) {
        return Response.status(500).entity(e.getMessage()).build();
    }
}

Maps exceptions to structured HTTP responses for REST endpoints.

Error Handling

WebApplicationException: Use for returning specific HTTP status codes in REST endpoints.
SOAPFaultException: Thrown when a SOAP request fails. Inspect fault code and message for debugging.
EndpointException: Occurs when the service endpoint fails to start or bind. Verify port availability and configuration.

Best Practices

Use annotations (@Path, @GET, @POST, @WebService) for clarity and standardization.

Leverage CXF interceptors for cross-cutting concerns like logging, metrics, and security.

Separate service interfaces and implementations for maintainability.

Use Spring or CDI integration for dependency injection.

Document REST endpoints using OpenAPI and SOAP services using WSDL.