Computing Magazine

How to Handle Exceptions in Jersey And Mule

Posted on the 13 March 2014 by Abhishek Somani @somaniabhi
This post describes how to handle exceptions in jersey component of mule .We can use ExceptionMappers for converting exception in jersey response . Here is the RestClass which throws a custom exception .

package com.test;
import java.io.File;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/application")
public class RestClass {


@GET
@Path("/exception")
@Produces(MediaType.TEXT_PLAIN)
public Response exceptionTest() throws Exception{
throw new MyException(401,"User is not Authorized");
}
}
Exception Class .

package com.test;
public class MyException extends Exception
{
private int statusCode ;
private String msg ;
public MyException(int statusCode , String msg)
{
this.statusCode = statusCode ;
this.msg = msg ;
}
public int getStatusCode()
{
return statusCode;
}
public void setStatusCode(int statusCode)
{
this.statusCode = statusCode;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
}
Here is the mapper class which will map our custom exception to JAX-RS response .

package com.test;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
public class JerseyExceptionMapper implements ExceptionMapper
{
@Override
public Response toResponse(MyException arg0)
{
System.out.println(arg0.getStatusCode());
return Response.status(arg0.getStatusCode()).entity(arg0.getMsg()).build();
}
}
Specify this exception mapper in jersey resource of mule flow .

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:servlet="http://www.mulesoft.org/schema/mule/servlet" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/servlet http://www.mulesoft.org/schema/mule/servlet/current/mule-servlet.xsd ">


<flow name="servletTestFlow1" doc:name="servletTestFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>

<jersey:resources doc:name="REST">
<component class="com.test.RestClass" ></component>
<jersey:exception-mapper class="com.test.JerseyExceptionMapper" />
</jersey:resources>

</flow>

</mule>


Back to Featured Articles on Logo Paperblog

Magazines