Spring MVC - Exception to view mapping using SimpleMappingExceptionResolver

[Last Updated: Feb 2, 2018]

SimpleMappingExceptionResolver is an implementation of HandlerExceptionResolver. It allows us to configure following parameters:

  • mapping exception class names to view names.
  • mapping view names to response status codes. It is done by using HttpServletResponse#setStatus(statusCode) method internally.
  • specifying a default exception view.
  • specifying a default response status code.

If interested, check out doResolveException() method of


JavaConfig class

Here we are going to register SimpleMappingExceptionResolver as a bean.

public class AppConfig {

    HandlerExceptionResolver errorHandler () {
        SimpleMappingExceptionResolver s =
                  new SimpleMappingExceptionResolver();

        //exception to view name mapping
        Properties p = new Properties();
        p.setProperty(NullPointerException.class.getName(), "npeView");

        //mapping status code with view response.
        s.addStatusCode("npeView", 404);

        //setting default error view
        //setting default status code

        return s;

    public ViewResolver viewResolver () {
        InternalResourceViewResolver viewResolver =
                  new InternalResourceViewResolver();
        return viewResolver;

The Controller

public class ExampleController {

    private DataService dataService;

    public String handleRequest (@PathVariable("orderId") String orderId) {
        return dataService.getOrderDetail(orderId);

    public String testHandler (@PathVariable("id") String id) {
        return dataService.getData(id);

    public void testHandler2 () {
       throw new RuntimeException("test exception");
public class DataService {
    private Map<String, Object> testData = new HashMap<>();
    private Map<Integer, String> testOrders = new HashMap<>();

    public DataService () {
        testData.put("1", "this is test data");
        testOrders.put(1, "Order 1");
        testOrders.put(2, "Order 2");

    public String getData (String id) {
        //we purposely created NPE scenario for demo
        //only id=1 will return test data
        return testData.get(id).toString();

    public String getOrderDetail (String orderId) {
        try {
            //only an integer is valid for order id
            int i = Integer.parseInt(orderId);
            String s = testOrders.get(i);
            return s == null ? "Empty Order" : s;
        } catch (NumberFormatException e) {
            throw new OrderIdNotValidException(
                      "Order id is not valid: " + orderId);

In above DataService class, we purposely created error scenarios.

public class OrderIdNotValidException extends RuntimeException {

    public OrderIdNotValidException (String message) {

JSP pages

<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"

<h3>This is default exception page</h3>
 <p>Exception: <b>${exception}</b></p>


Note that, in above JSP page, we are using ${exception} place holder for the exception object. SimpleMappingExceptionResolver, by default, uses the Model attribute, "exception", mapped to the exception object. This default attribute name can be changed by using:
SimpleMappingExceptionResolver #setExceptionAttribute(String exceptionAttribute)

<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"

<h4>Not a valid Order Id</h3>
<p>The provided order Id is not valid:  ${orderId}</p>
<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"

<h4>Data not found</h3>
<p>No data found for id ${id}</p>

Running embedded tomcat

 mvn clean install tomcat7:run-war


We are going to use HTTPie to test output. HTTPie is a command line http client. It shows status code , headers and raw body content. If interested, checkout the tutorial to learn more about HTTPie.

Testing: /order/{orderId}

According to our DataService logic, only an integer is valid order id. Invalid order id will throw OrderIdNotValidException, which we mapped to OrderIdNotValidView.jsp in our SimpleMappingExceptionResolver bean.

Testing: /data/{id}

In DataService#getData(), only id=1 will return the data, others will throw NullPointerException. This exception is mapped to npeView.jsp. We also mapped this view with response status code 404:

Testing: /test

The mapped handler is throwing RuntimeException, which is not mapped to any view in our SimpleMappingExceptionResolver bean. In that case default error view will be shown and default status code will be returned in response:

Other URIs

Other URIs, which are not mapped to any handler will give tomcat 404 page.

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.5.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.0.1 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Simple Mapping Exception Resolver Example Select All Download
  • simple-mapping-exception-resolver
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • webapp
            • WEB-INF
              • views

    See Also