<!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;">&#160;</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;">&#160;</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;">&#160;</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;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Create a exception handler/mapper to marshal the exception.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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&lt;Exception&gt; {


&#160;&#160;&#160; @Override
&#160;&#160;&#160; public Response toResponse(Exception e) {&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; StringBuilder response = new StringBuilder("&lt;response&gt;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.append("&lt;status&gt;ERROR&lt;/status&gt;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.append("&lt;message&gt;" + e.getMessage() + "&lt;/message&gt;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.append("&lt;/response&gt;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160; return Response.serverError().entity(response.toString()).type(MediaType.APPLICATION_XML).build();
&#160;&#160;&#160; }
}
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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;">&#160;</p><pre class="jive-pre"><code class="jive-code">&lt;context-param&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;param-name&gt;resteasy.providers&lt;/param-name&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;param-value&gt;com.nuwan.poc.wsmodule.exception.DefaultExceptionHandler&lt;/param-value&gt;
&lt;/context-param&gt;</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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;">&#160;</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 {
&#160;&#160;&#160; private long id;
&#160;&#160;&#160; private String status;
&#160;&#160;&#160; private String message;


&#160;&#160;&#160; public long getId() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (new Date()).getTime();
&#160;&#160;&#160; }


&#160;&#160;&#160; @XmlAttribute
&#160;&#160;&#160; public void setId(long id) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.id = id;
&#160;&#160;&#160; }


&#160;&#160;&#160; public String getStatus() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; return status;
&#160;&#160;&#160; }


&#160;&#160;&#160; @XmlElement
&#160;&#160;&#160; public void setStatus(String status) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.status = status;
&#160;&#160;&#160; }


&#160;&#160;&#160; public String getMessage() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; return message;
&#160;&#160;&#160; }


&#160;&#160;&#160; @XmlElement
&#160;&#160;&#160; public void setMessage(String message) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.message = message;
&#160;&#160;&#160; }
}

</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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;">&#160;</p><pre class="jive-pre"><code class="jive-code">
&#160;&#160;&#160; @GET
&#160;&#160;&#160; @Path("testWebService")
&#160;&#160;&#160; @Produces({MediaType.APPLICATION_XML})
&#160;&#160;&#160; public Response testWebService(int value) throws Exception {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response response = new Response();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if((100 / value) &gt; 0) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.setStatus("OK");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.setMessage("Greater Than Zero");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.setStatus("OK");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.setMessage("Less Than Zero");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160; catch (ArithmeticException e) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new ArithmeticException("Division by zero!");
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160; return response;
&#160;&#160;&#160; }

</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">&lt;response&gt;
&#160; &lt;status&gt;ERROR&lt;/status&gt; 
&#160; &lt;message&gt;Division by zero!&lt;/message&gt; 
&lt;/response&gt;</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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>