[JAX-RS] ExceptionMapper example (Maven + Jersey + Glassfish)

Create a new Maven project (Web app archetype) in eclipse and add the dependencies on jersey. Also you will need Glassfish server.
1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.vvirlan</groupId>
	<artifactId>rsexampleresponse</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>rsexampleresponse Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.19</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-grizzly2</artifactId>
			<version>1.19</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>rsexampleresponse</finalName>
	</build>
</project>

2. Main Application

package com.vvirlan;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.vvirlan.exception.IllegalArgumentExceptionMapper;
import com.vvirlan.resources.ExceptionResource;
import com.vvirlan.resources.HelloResource;

@ApplicationPath("rest")
public class ApplicationMain extends Application {

	@Override
	public Set<Class<?>> getClasses() {
		Set<Class<?>> classes = new HashSet<Class<?>>();
		classes.add(ExceptionResource.class);
//Note that you need to add the exception mapper here, otherwise, it won't work
		classes.add(IllegalArgumentExceptionMapper.class);
		return classes;
	}
}

3. An example service

package com.vvirlan.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

@Path("e")
public class ExceptionResource {
//This is the default implementation	
	@GET
	@Path("nf")
	public Response getException () {
		throw new WebApplicationException(Response.Status.CONFLICT);
	}
	
//Throw the IllegalArgument and it will get mapped automatically to whatever you define in the mapper (next class)
	@GET
	@Path("il")
	public Response getIllegal() {
		throw new IllegalArgumentException();
	}
}

4. The Exception Mapping class. Here we’re mapping the IllegalArgumentException to a BAD_REQUEST

package com.vvirlan.exception;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> {

	public Response toResponse(IllegalArgumentException arg0) {
		return Response.status(Status.BAD_REQUEST).build();
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s