Site icon

Jersey ExceptionMapper (standard exceptions) in RESTful web Service (java/ example)

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:

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: 

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:

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:

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).

  1. Request : http://localhost:9095/exp/outofbound
    Response : Catching in ApplicaitonExceptionMapper : String index out of range: 5
  2. Request : http://localhost:9095/exp/nullp
    Response : Catching in NullPointerExceptionMapper : Throwing null pointer exception
  3. Request : http://localhost:9095/exp/dividebyzero
    Response : Catching in ArithmeticExceptionMapper : / by zero

Download code – RESTFul web service Jersey ExceptionMapper Example

 

 

Exit mobile version