CXF SOAP Exception Handling

CXF SOAP Exception Handling explains about step by step details of Exception Handling In CXF SOAP Services

Apache CXF is a free and open source project, and a fully featured Webservice framework. It helps you building webservices using different front-end API's, like as JAX-RS and JAX-WS.

Apache CXF provides in built exception handling mechanism for both REST & SOAP based services, By using this you can handle exceptions very easily without any additional efforts.

On this following example, we are showing how to Implement CXF Exception Handling for SOAP based services, This tutorial is based on CXF 2.7.3


You can also see the CXF REST & SOAP Exception Handling Together, please refer CXF REST & SOAP Exception Handling, For Restful exception handling, please refer CXF Restful Exception Handling

Required Libraries

You need to download

  1. OpenJDK 8 / Corretto
  2. Eclipse 4.15
  3. CXF-3.3.6
  4. Tomcat 9

Following jar must be in classpath

  1. cxf-core-3.3.6.jar
  2. cxf-rt-bindings-soap-3.3.6.jar
  3. cxf-rt-databinding-jaxb-3.3.6.jar
  4. cxf-rt-features-logging-3.3.6.jar
  5. cxf-rt-frontend-jaxws-3.3.6.jar
  6. cxf-rt-frontend-simple-3.3.6.jar
  7. cxf-rt-transports-http-3.3.6.jar
  8. cxf-rt-wsdl-3.3.6.jar
  9. httpasyncclient-4.1.4.jar
  10. httpclient-4.5.12.jar
  11. httpcore-4.4.13.jar
  12. httpcore-nio-4.4.13.jar
  13. neethi-3.1.1.jar
  14. slf4j-api-1.7.29.jar
  15. slf4j-jdk14-1.7.29.jar
  16. spring-aop-5.1.14.RELEASE.jar
  17. spring-beans-5.1.14.RELEASE.jar
  18. spring-context-5.1.14.RELEASE.jar
  19. spring-core-5.1.14.RELEASE.jar
  20. spring-expression-5.1.14.RELEASE.jar
  21. spring-jcl-5.1.14.RELEASE.jar
  22. spring-web-5.1.14.RELEASE.jar
  23. stax2-api-3.1.4.jar
  24. woodstox-core-5.0.3.jar
  25. wsdl4j-1.6.3.jar
  26. xmlschema-core-2.2.5.jar

CXF Exception Handling Example

I am creating a sample web service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFTutorial" according to following screenshot

CXF Exception Handling CXF Exception Handling

Create a Student Object

package com.student;

public class Student {
private String name;
public String getName() {
return name;
public void setName(String name) { = name;

Create Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import javax.jws.WebService;

public interface ChangeStudentDetails {
Student changeName(Student student) throws ServiceException;

Create a ServiceExceptionDetails

This class holds the exception details for a service

package com.student;


public class ServiceExceptionDetails implements Serializable {

private String faultCode;
private String faultMessage;

public ServiceExceptionDetails() {

public String getFaultCode() {
return faultCode;

public void setFaultCode(String faultCode) {
this.faultCode = faultCode;

public String getFaultMessage() {
return faultMessage;

public void setFaultMessage(String faultMessage) {
this.faultMessage = faultMessage;


Create a ServiceException

This class(ServiceException) extending Exception, This class contains an array of ServiceExceptionDetails (if more than one exceptions occurred)

package com.student;

import javax.xml.bind.annotation.XmlRootElement;

public class ServiceException extends Exception implements Serializable {
private ServiceExceptionDetails faultDetails[];

public ServiceException(ServiceExceptionDetails faultDetails[]) {
this.faultDetails = faultDetails;

public ServiceException(String message, ServiceExceptionDetails faultDetails[]) {
this.faultDetails = faultDetails;

public ServiceExceptionDetails[] getFaultDetails() {
return faultDetails;


Implement the Service Interface

Here we implement the service interface created on the previous step

package com.student;

import javax.jws.WebService;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

public Student changeName(Student student) throws ServiceException {
if (student.getName().equals("Rockey")) {
student.setName((new StringBuilder("HELLO ")).append(student.getName()).toString());
return student;
} else {
ServiceExceptionDetails ServiceExceptionDetailsArray[] = new ServiceExceptionDetails[1];
      ServiceExceptionDetails serviceExceptionDetails =
new ServiceExceptionDetails();
("Student Name is not correct");
[0] = serviceExceptionDetails;
throw new ServiceException("Fault Message", ServiceExceptionDetailsArray);


CXF is using Spring internally, Finding classes by spring we need to add service implementation class on "jaxws:endpoint" tag

<beans xmlns=""
    xmlns:xsi="" xmlns:jaxws=""

    <jaxws:endpoint id="changeStudent"
        implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent" />



Change the web.xml file to find CXF servlet and cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
    xmlns="" xmlns:web=""
    id="WebApp_ID" version="3.0">




Publishing CXF Web Service

CXF Exception Handling


You can Find all the deployed JAX-WS/JAX-RS services you need to append 'services' at the end of the URL so URL will become following


Deployed CXF Web Service

CXF Exception Handling

you need to generate proxy for running this service, see Java wsimport Tool Example in order to run this service

Run Client

package com.client;

import java.util.List;
import com.student.ChangeStudentDetails;
import com.student.ChangeStudentDetailsImplService;
import com.student.ServiceExceptionDetails;
import com.student.ServiceException_Exception;
import com.student.Student;
//CXF Exception Handling Example
public class Main {
public static void main(String[] args) {
ChangeStudentDetailsImplService service = new ChangeStudentDetailsImplService();
    ChangeStudentDetails changeStudentDetailsImplPort = service.getChangeStudentDetailsImplPort

    Student student =
new Student();
try {
student = changeStudentDetailsImplPort.changeName(student);
} catch (ServiceException_Exception e) {
List faultDetails = e.getFaultInfo().getFaultDetails();
for (ServiceExceptionDetails serviceExceptionDetails : faultDetails) {
System.out.println("Fault code = " + serviceExceptionDetails.getFaultCode() + "\nFault message = "
+ serviceExceptionDetails.getFaultMessage());


When you invoke the above client with student name as Rockey, you will get the correct response

HELLO Rockey

When you invoke the above client with student name other than Rockey, you will get a ServiceExceptionDetails

Fault code = 100
Fault message = Student Name is not correct


2 Responses to "CXF SOAP Exception Handling"
  1. Rony 2012-11-10 09:29:22.0
  1. santosh sahu 2012-11-11 09:29:22.0

Your email address will not be published. Required fields are marked *