[jboss-dev-forums] [JBoss Web Services Development] - RESTEasy
Nuwan Bandara
do-not-reply at jboss.com
Mon Feb 4 14:05:54 EST 2013
Nuwan Bandara [https://community.jboss.org/people/Daffy] modified the document:
"RESTEasy"
To view the document, visit: https://community.jboss.org/docs/DOC-48310
--------------------------------------------------------------
h2. Purpose
Purpose of the article is to show how to handle exception in RESTEasy.
h2. Solution
RESTEasy provides such a exception handling mechanism which simplifies the exception handling process. 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.
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.
h2. How to Do?
h4. High-Level Steps
* Implements ExceptionMapper
* Register ExceptionMapper in the web.xml
*
Create a exception handler/mapper to marshal the exception.
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();
}
}
Register the DefaultExceptionHandler in your web.xml
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.nuwan.poc.wsmodule.exception.DefaultExceptionHandler</param-value>
</context-param>
Create JAXB model class for marshalling POJO to XML to produce the response. (This is optional)
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;
}
}
. Here's the RESTEasy web service method implementation. When the "value" is 0, code hit the catch block and throw an ArithmeticException. Which is a type of an Exception that we have already handled in DefaultExceptionHandler class.
@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;
}
* Result of this exception will produce an out put the following xml response
<response>
<status>ERROR</status>
<message>Division by zero!</message>
</response>
h2. Reference
* http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/ExceptionHandling.html#builtinException Chapter 25. Exception Handling
--------------------------------------------------------------
Comment by going to Community
[https://community.jboss.org/docs/DOC-48310]
Create a new document in JBoss Web Services Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2047]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20130204/059ce7f0/attachment.html
More information about the jboss-dev-forums
mailing list