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
Required Libraries
You need to download
Following jar must be in classpath
- cxf-core-3.3.6.jar
- cxf-rt-bindings-soap-3.3.6.jar
- cxf-rt-databinding-jaxb-3.3.6.jar
- cxf-rt-features-logging-3.3.6.jar
- cxf-rt-frontend-jaxws-3.3.6.jar
- cxf-rt-frontend-simple-3.3.6.jar
- cxf-rt-transports-http-3.3.6.jar
- cxf-rt-wsdl-3.3.6.jar
- httpasyncclient-4.1.4.jar
- httpclient-4.5.12.jar
- httpcore-4.4.13.jar
- httpcore-nio-4.4.13.jar
- neethi-3.1.1.jar
- slf4j-api-1.7.29.jar
- slf4j-jdk14-1.7.29.jar
- spring-aop-5.1.14.RELEASE.jar
- spring-beans-5.1.14.RELEASE.jar
- spring-context-5.1.14.RELEASE.jar
- spring-core-5.1.14.RELEASE.jar
- spring-expression-5.1.14.RELEASE.jar
- spring-jcl-5.1.14.RELEASE.jar
- spring-web-5.1.14.RELEASE.jar
- stax2-api-3.1.4.jar
- woodstox-core-5.0.3.jar
- wsdl4j-1.6.3.jar
- 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
Create a Student Object
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
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
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)
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
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
Deployed CXF Web Service
you need to generate proxy for running this service, see Java wsimport Tool Example in order to run this service
Run 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