- In this post, we will discuss jersey exception mappers to catch standard exceptions.
- Exception handling is need of our applications. The jersey ExceptionMapper provides the cross cutting concern.
- We can able to catch all application exception at one place. we do not need to handle the exceptions in each and every function.
- We have discussed about user defined exceptions in Jersey ExceptionMapper Example.
- In this post, we will throw and catch following standard exceptions.
- NullPointerException
- ArithmeticException
- Index out of bound (StringIndexOutOfBoundsException)
- We will write exception mapper for NullPointerException & ArithmeticException
- i.e. NullPointerExceptionMapper & ArithmeticExceptionMapper.
- We will write generic ApplicationExceptionMapper, which will catch all application exceptions includingStringIndexOutOfBoundsException.
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 in java
<?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 – ExceptionMapper in RESTFul web service (jersey/java/example).
1.) ExpResource class:
- We have create REST resource, capable of exposing GET APIs.
- We are throwing following exceptions from REST resources.
- NullPointerException
- ArithmeticException
- Index out of bound (StringIndexOutOfBoundsException)
- The exceptions will be caught by exceptionmapper classes.
package org.learn.resource; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @Path("/exp") public class ExpResource { @GET @Path("outofbound") public Response indexOutOfBoundException() { String indexOutOfBound = "hello"; //It will raise index out of bound exception indexOutOfBound.charAt(indexOutOfBound.length()); return Response.status(Response.Status.OK).build(); } @GET @Path("nullp") public Response nullPointerException() { throw new NullPointerException("Throwing null pointer exception"); } @GET @Path("dividebyzero") public Response divideByZero() { int divByZero = 100 / 0; System.out.println(divByZero); return Response.status(Response.Status.OK).build(); } }
2.) NullPointerExceptionMapper class:
- NullPointerExceptionMapper class catch NullPointerExceptions thrown by our application.
package org.learn.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; @Provider public class NullPointerExceptionMapper implements ExceptionMapper<NullPointerException> { @Override public Response toResponse(NullPointerException e) { return Response .status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) .type(MediaType.TEXT_PLAIN) .entity("Catching NullPointerExceptionMapper : "+ e.getMessage()) .build(); } }
3.) ArithmeticExceptionMapper class:
- ArithmeticExceptionMapper class catch ArithmeticException thrown by our application
- In ExpResource, we are throwing arithmetic exception (divide by zero).
- ArithmeticExceptionMapper will catch ArithmeticException.
package org.learn.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; @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 ArithmeticExceptionMapper : "+ e.getMessage()) .build(); } }
4.) ApplicationExceptionMapper class:
- ApplicationExceptionMapper class is capable of handling all exceptions, which are inherited for Exception class.
- In ExpResource class, we are throwing index out of bound exception.
package org.learn.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; @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 ApplicaitonExceptionMapper : " + e.getMessage()) .build(); } }
Dependencies: The dependencies defined in our pom file is as follows.
junit junit 3.8.1 test org.glassfish.jersey.containers jersey-container-servlet ${jersey_version} org.glassfish.jersey.core jersey-client ${jersey_version} javax.servlet javax.servlet-api ${servlet_api_version}
Output – ExceptionMapper in RESTFul web service (Jersey /Java/ Example).
- We have deployed our application at context root.
- We have used Chrome browser to raise request to our resource.
- 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 NullPointerExceptionMapper : Throwing null pointer exception - Request : http://localhost:9095/exp/dividebyzero
Response : Catching in ArithmeticExceptionMapper : / by zero
Download code – RESTFul web service Jersey ExceptionMapper Example