CXF REST Exception Handling

Create a Service Interface

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

package com.student;

public interface ChangeStudentDetails {

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

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "ServiceExceptionDetails")
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 contains an array of ServiceExceptionDetails(if more than one exceptions occurred)

package com.student;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "ServiceException")
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

Here we are using one example showing with GET method& another with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

package com.student;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Consumes("application/json")
@Produces("application/json")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) throws ServiceException {
   
if (student.getName().equals("Tom")) {
     
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;
      throwException
(new ServiceException(serviceExceptionDetailsArray));
     
return null;
   
}
  }

 
public static void throwException(ServiceException serviceException) throws ServiceException {
   
ResponseBuilder builder = Response.status(Response.Status.NOT_ACCEPTABLE);
    builder.type
("application/json");
    builder.entity
(serviceException.getFaultDetails());
   
throw new WebApplicationException(builder.build());
 
}
}




Previous 1 2 3 4 Next





3 Responses to "CXF REST Exception Handling"
  1. harry 2012-11-17 08:30:35.0
  1. admin 2012-11-18 08:30:35.0
  1. Joe 2012-11-19 08:30:35.0

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