Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part III (Exception/Error handling using ExceptionMapper)

This post will explain handling errors and exceptions in RESTFul webservices developed with Apache CXF using JAX-RS’s ExceptionMapper.

Part I – A simple RESTful Webservices using Apache CXF
Part II – Testing using RestTemplate and JUNIT
Part III – Exception/Error handling using ExceptionMapper
Part IV – BeanParam Example

By having implementations of ExceptionMapper to handle exceptions and errors, the service can provide a meaningful JSON response instead of stack trace to the clients. It also provides other capabilities like logging, sending notifications etc.,

In this example, we will have ExceptionMapper implementations for the following,

ExceptionMapper for ClientErrorException (ClientExceptionMapper.java)


package com.idodevjobs.sample.exception.mapper;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class ClientExceptionMapper implements ExceptionMapper<ClientErrorException> {
    @Override
    public Response toResponse(ClientErrorException e) {
        return Response.status(Response.Status.NOT_FOUND).header("Content-Type", "application/json").entity(new String("NOT FOUND")).build();
    }
}


ExceptionMapper for ServerErrorException (ServerExceptionMapper.java)


package com.idodevjobs.sample.exception.mapper;

import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class ServerExceptionMapper implements ExceptionMapper<ServerErrorException> {
    @Override
    public Response toResponse(ServerErrorException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).header("Content-Type", "application/json").entity(new String("INTERNAL SERVER ERROR")).build();
    }
}


ExceptionMapper for Throwable (ThrowableMapper.java)


package com.idodevjobs.sample.exception.mapper;

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

public class ThrowableMapper implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).header("Content-Type", "application/json").entity(new String("DATA ERROR")).build();
    }
}


Spring/CXF Configuration(spring-cxf-rest-example.xml)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

    <context:component-scan base-package="com.idodevjobs" />

    <jaxrs:server id="exampleCxfServer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="exampleService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
        </jaxrs:extensionMappings>
        <jaxrs:features>
            <cxf:logging/>
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
            <bean class="com.idodevjobs.sample.exception.mapper.ClientExceptionMapper" />
            <bean class="com.idodevjobs.sample.exception.mapper.ServerExceptionMapper" />
            <bean class="com.idodevjobs.sample.exception.mapper.ThrowableMapper" />
        </jaxrs:providers>
    </jaxrs:server>

</beans>

The ExceptionMapper implementation classes have to be listed under providers in the configuration xml.

Refer to my previous post for other required classes and files.

The war has to be built and deployed on tomcat/jetty for testing. This can be tested using any REST client.

Please share if you find this useful.

Please comment for any questions/issues.

Advertisements
This entry was posted in java and tagged , , , , , , . Bookmark the permalink.

6 Responses to Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part III (Exception/Error handling using ExceptionMapper)

  1. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part I | iDoDevJobs

  2. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part II (Testing using RestTemplate and JUNIT) | iDoDevJobs

  3. there are formatting issues with and “. And you forgot to add

    in test-context.xml in the third step. otherwiese those mappes are not activated

    Like

  4. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part IV (JAX-RS’ BeanParam Example) | iDoDevJobs

  5. Anand says:

    Does it work even for the exceptions raised from interceptors?

    I have a REST application built using Apache CXF 3.1.4. In my experience, the mapper is not invoked when an exception is thrown from an interceptor.

    TIA for any help/guidelines in this regard.

    Like

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