- In this post, we will discuss Jersey (RESTFul Service) Exception mapper example for user defined or custom exceptions.
- We have already discussed, usage of standard exceptions in ExceptionMapper – RESTFul web Service.
- We will create couple of user defined exceptions
- MyNullPointerException (extending NullPointerException).
- MyArithmeticException (extending ArithmeticException).
- We will define following exceptionmappers to catch application & user defined exceptions.
- ApplicationExceptionMapper will catch all application exceptions.
- ArithmeticExceptionMapper will catch all sort of Arithmetic (or MyArithmeticException) exceptions.
- MyNullPointerExceptionMapper will catch NullPointer exceptions (or MyNullPointerException).
Jersey maven dependency
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.23.2</version> </dependency>
Web.xml – jersey RESTful web Service
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>REST Web Application using Jersey framework</display-name>
<servlet>
<servlet-name>rest-server</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.learn</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-server</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Program – Jersey ExceptionMapper in RESTFul Web Service (User defined exception)
1.) MyNullPointerException class (Custom exception):
- MyNullPointerException class extends NullPointerException class.
package org.learn.exception;
public class MyNullPointerException extends NullPointerException {
private static final long serialVersionUID = 5627096569547521249L;
public MyNullPointerException(String s) {
super(s);
}
}
2.) MyArithmeticException class (Custom exception) :
- MyArithmeticException class extends ArithmeticException class.
package org.learn.exception;
public class MyArithmeticException extends ArithmeticException {
private static final long serialVersionUID = 652325545058196579L;
public MyArithmeticException(String s) {
super(s);
}
}
3.) ApplicationExceptionMapper class:
- ApplicationExceptionMapper will catch exceptions extending from Exception class.
- Any uncaught exception will be caught by ApplicaitonExceptionMapper.
- As we have specialized the exception mappers to catch NullPointer & Arithmetic exception, so
- NullPointerException will be caught by MyNullPointerException.
- ArithmeticException will be caught by MyArithmeticException
package org.learn.exceptionmapper;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class ApplicationExceptionMapper implements ExceptionMapper<Exception> {
public Response toResponse(Exception e) {
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
.type(MediaType.TEXT_PLAIN)
.entity("Catching in ApplicaitonExceptionMapper : " + e.getMessage())
.build();
}
}
4.) ArithmeticExceptionMapper class:
- ArithmeticExceptionMapper will catch all Arithmetic exceptions.
- Our user defined exception MyArithmeticException will also by caught of ArithmeticException mapper only.
package org.learn.exceptionmapper;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class ArithmeticExceptionMapper implements ExceptionMapper<ArithmeticException> {
@Override
public Response toResponse(ArithmeticException e) {
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
.type(MediaType.TEXT_PLAIN)
.entity("Catching in ArithmeticExceptionMapper : "+ e.getMessage())
.build();
}
}
5.) MyNullPointerExceptionMapper class:
- MyNullPointerExceptionMapper will catch exceptions of type MyNullPointerException.
- When MyNullPointerException is thrown from our resource class, It will be caught in MyNullPointerExceptionMapper class.
package org.learn.exceptionmapper;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.learn.exception.MyNullPointerException;
@Provider
public class MyNullPointerExceptionMapper implements ExceptionMapper<MyNullPointerException> {
@Override
public Response toResponse(MyNullPointerException e) {
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
.type(MediaType.TEXT_PLAIN)
.entity("Catching in MyNullPointerExceptionMapper : "+ e.getMessage())
.build();
}
}
6.) Resource class (REST resource):
Resource class is our REST resource, where we have exposed the following APIs.
- /outofbound: In this api, we are accessing the array beyond its bounds so its length, so StringIndexOutOfBoundsException exception will be thrown.
- As we have not created any specific exception mapper, so It will be caught in ApplicationExceptionMapper class.
- /nullp: We are throwing null pointer exception here. It will be caught in MyNullPointerExceptionMapper.
- /dividebyzero: We are throwing MyArithmeticException from this resource, It will be caught in ArithmeticExceptionMapper.
package org.learn.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.learn.exception.MyArithmeticException;
import org.learn.exception.MyNullPointerException;
@Path("/exp")
public class Resource {
@GET
@Path("outofbound")
public Response indexOutOfBoundException() {
String indexOutOfBound = "hello";
/*
Any code come here and suppose following exception raised
*/
//It will raise index out of bound exception
//It will be caught in ApplicationExceptionMapper
indexOutOfBound.charAt(indexOutOfBound.length());
return Response.status(Response.Status.OK).build();
}
@GET
@Path("nullp")
public Response nullPointerException() {
/*
Any code come here and suppose following exception raised
*/
//It will be caught in MyNullPointerExceptionMapper
throw new MyNullPointerException("Throwing my null pointer exception");
}
@GET
@Path("dividebyzero")
public Response divideByZero() {
/*
Any code come here and suppose following exception raised
* */
//Raising divide by zero exception
int divByZero = 0;
try {
divByZero = 100 / 0;
} catch(ArithmeticException exp) {
//It will be caught in yArithmeticExceptionMapper
throw new MyArithmeticException("Throwing MyArithmeticException "+exp.getMessage());
}
System.out.println(divByZero);
return Response.status(Response.Status.OK).build();
}
}
Output – Jersey ExceptionMapper in RESTFul Web Service (User defined exception)
- We have deployed our application at context root.
- As, we have three method defined in our resource class, we have capture their request response as follows:
Request : http://localhost:9095/exp/outofbound Response : Catching in ApplicaitonExceptionMapper : String index out of range: 5 Request : http://localhost:9095/exp/nullp Response : Catching in MyNullPointerExceptionMapper : Throwing my null pointer exception Request : http://localhost:9095/exp/dividebyzero Response : Catching in ArithmeticExceptionMapper : Throwing MyArithmeticException / by zero.
Download Code – Jersey ExceptionMapper in RESTFul Web Service