title: Catching exceptions from a Jersey REST client thrown from a Spring MVC Rest service
body: <p>I implemented a REST application using Spring MVC, Jersey and JAXB. </p>
  
  <p>The client sends a <code>ServiceRequest</code> object which contains information about the request and receives a <code>ServiceResponse</code> object back with information about the response. </p>
  
  <p>ServiceRequest</p>
  
  <pre><code>@XmlRootElement(name = "servicerequest")
  public class ServiceRequest{
  
      String serviceName = "AddUser"
  
      public String getServiceName() {
          return serviceName;
      }
  
      @XmlElement
      public void setServiceName(String serviceName) {
          this.serviceName = name;
      }   
  }
  </code></pre>
  
  <p>ServiceResponse</p>
  
  <pre><code>@XmlRootElement(name = "serviceresponse")
  public class ServiceResponse {
  
      String responseCode; 
  
      public String getResponseCode() {
          return responseCode;
      }
  
      @XmlElement
      public void setResponseCode(String code) {
          this.responseCode = name;
      }   
  }
  </code></pre>
  
  <p>The client makes the call to the service using Jersey</p>
  
  <p>ServiceClient</p>
  
  <pre><code>ClientConfig config = new DefaultClientConfig();
  Client client = Client.create(config);
  WebResource service = client.resource(RESOURCE_URL);
  
  ServiceRequest request = new ServiceRequest();
  ServiceResponse response = service.path("addUser").type(MediaType.APPLICATION_XML)
  .accept(MediaType.APPLICATION_XML).entity(request).post(ServiceRequest.class);
  </code></pre>
  
  <p>On the server side, the service gets the request, handles it and returns a <code>ServiceResponse</code> object. </p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, 
              value = "/addUser",headers="Accept=application/xml")
      @ResponseStatus(HttpStatus.OK)  
  public @ResponseBody ServiceResponse addUser(@RequestBody ServiceRequest request) {
  
          ServiceResponse response = new ServiceResponse();
  
          //Handle request
          //service.addUser();
  
          response.setCode("200");
          return response; 
  }
  </code></pre>
  
  <p>The controller class shown above makes a call to another service class to handle the request (<code>service.addUser()</code>). This service class can raise a number of exceptions.</p>
  
  <p>What i am not sure of is how best to handle them. After <em>googling</em> around, i found that i can use an ExceptionHandler as shown below:</p>
  
  <pre><code>@ExceptionHandler(NullPointerException.class)
  @ResponseBody
  public String handleException1(NullPointerException ex)
  {
      return ex.getMessage();
  }
  </code></pre>
  
  <p>I can have the controllers extend a base class that has the exception handler. </p>
  
  <p>A couple of questions regarding the above approach:</p>
  
  <ul>
  <li>Do i have to create one handler for each exception type? There are so many exceptions that i need to handle so is it not possible to just have a generic handler for all exceptions?</li>
  <li>As my controller returns an object of type <code>ServiceResponse</code>, what would be the object type that would be returned when an exception occurs? </li>
  <li>The client expects to get an object of <code>ServiceResponse</code> type. How does it work if the response is an exception type?</li>
  </ul>
  
  <p>And finally, i noticed that the ExceptionHanlder can only have a return type that is one of the following:</p>
  
  <pre><code>ModelAndView
  Model
  Map
  View
  String – interpreted as a view name
  void, but only if the method writes directly to the response object
  </code></pre>
  
  <p>I thought i can create the <code>ServiceResponse</code> object in the <code>ExceptionHandler</code> and set the appropriate code and return the <code>ServiceResponse</code> object. This however is not possible if i cant return a <code>ServiceResponse</code> type in the excpetion handler.</p>
  
  <h2>Edit</h2>
  
  <p>I tried using the ExceptionHandler using a generic exception class as shown below</p>
  
  <pre><code>@ExceptionHandler(ServiceException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
      public @ResponseBody resolveServiceValidationException(ServiceException ex) {       
  
          System.out.println("In resolveServiceError");
  
          ServiceResponse = new ServiceResponse();
          response.setResponseException(ex.getMessage());
          return response;        
      } 
  </code></pre>
  
  <p>The exception is being caught but the ServiceResponse object is always null. All i get is the following message:</p>
  
  <pre><code>GET http://localhost:8080/myService/services/pingError returned a response status of 400 Bad Request
  </code></pre>
  
  <p>How can i access the response object when an exception is returned back?</p>
  
  <p>Thanks</p>