<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
RESTEasy
</h3>
<span style="margin-bottom: 10px;">
modified by <a href="https://community.jboss.org/people/Daffy">Nuwan Bandara</a> in <i>JBoss Web Services Development</i> - <a href="https://community.jboss.org/docs/DOC-48310">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><h2>Purpose</h2><p>Purpose of the article is to show how to handle exception in RESTEasy. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>Solution</h2><p><span style="font-size: 10pt;">RESTEasy provides such a exception handling mechanism which simplifies the exception handling process. </span><span style="font-size: 10pt;">RESTEasy ExceptionMappers are custom, application provided, components that can catch thrown application exceptions and write specific HTTP responses. The are classes annotated with @Provider and that implement this interface.</span> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>When an application exception is thrown it will be caught by the JAX-RS runtime. JAX-RS will then scan registered ExceptionMappers to see which one support marshalling the exception type thrown.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>How to Do?</h2><h4>High-Level Steps</h4><ul><li>Implements <span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">ExceptionMapper</span></li><li><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">Register <span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">ExceptionMapper</span> in the web.xml<br/></span></li><li><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;"><br/></span></li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Create a exception handler/mapper to marshal the exception.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">package com.nuwan.poc.wsmodule.exception;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
* User: Nuwan.N.Bandara
*/
@Provider
public class DefaultExceptionHandler implements ExceptionMapper<Exception> {
    @Override
    public Response toResponse(Exception e) {       
        StringBuilder response = new StringBuilder("<response>");
        response.append("<status>ERROR</status>");
        response.append("<message>" + e.getMessage() + "</message>");
        response.append("</response>");
        return Response.serverError().entity(response.toString()).type(MediaType.APPLICATION_XML).build();
    }
}
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Register the <span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">DefaultExceptionHandler </span>in your web.xml</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code"><context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>com.nuwan.poc.wsmodule.exception.DefaultExceptionHandler</param-value>
</context-param></code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Create JAXB model class for marshalling POJO to XML to produce the response. (This is optional)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">package com.nuwan.poc.wsmodule.model;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.Date;
/**
* User: Nuwan.N.Bandara
*/
@XmlRootElement
public class Response {
    private long id;
    private String status;
    private String message;
    public long getId() {
        return (new Date()).getTime();
    }
    @XmlAttribute
    public void setId(long id) {
        this.id = id;
    }
    public String getStatus() {
        return status;
    }
    @XmlElement
    public void setStatus(String status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    @XmlElement
    public void setMessage(String message) {
        this.message = message;
    }
}
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><li style="padding-left: 30px;">Here's the RESTEasy web service method implementation. When the "value" is 0, code hit the catch block and throw an <span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">ArithmeticException</span>. Which is a type of an Exception that we have already handled in <span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">DefaultExceptionHandler class.</span></li><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">
    @GET
    @Path("testWebService")
    @Produces({MediaType.APPLICATION_XML})
    public Response testWebService(int value) throws Exception {
        Response response = new Response();
        try {
            if((100 / value) > 0) {
                response.setStatus("OK");
                response.setMessage("Greater Than Zero");
            }
            else {
                response.setStatus("OK");
                response.setMessage("Less Than Zero");
            }
        }
        catch (ArithmeticException e) {
            throw new ArithmeticException("Division by zero!");
        }
        return response;
    }
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><ul><li>Result of this exception will produce an out put the following xml response</li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code"><response>
  <status>ERROR</status>
  <message>Division by zero!</message>
</response></code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>Reference</h2><ul><li><a class="jive-link-external-small" href="http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/ExceptionHandling.html#builtinException" rel="nofollow">Chapter 25. Exception Handling</a></li></ul></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-48310">going to Community</a></p>
        <p style="margin: 0;">Create a new document in JBoss Web Services Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2047">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>