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

Note

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) {
   
this.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;

@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;

import java.io.Serializable;

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 java.io.Serializable;
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[]) {
   
super(message);
   
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();
      serviceExceptionDetails.setFaultCode
("100");
      serviceExceptionDetails.setFaultMessage
("Student Name is not correct");
      ServiceExceptionDetailsArray
[0] = serviceExceptionDetails;
     
throw new ServiceException("Fault Message", ServiceExceptionDetailsArray);
   
}
  }
}

cxf.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

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

</beans>

web.xml

Change the web.xml file to find CXF servlet and cxf.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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/cxf.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Publishing CXF Web Service

CXF Exception Handling

Note

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

http://localhost:8080/CXFTutorial/services

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();
    student.setName
("Rockey");
   
try {
     
student = changeStudentDetailsImplPort.changeName(student);
      System.out.println
(student.getName());
   
} 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

Output
HELLO Rockey

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

Output
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 *