Close

Spring MVC - SimpleMappingExceptionResolver Examples

[Last Updated: Nov 8, 2025]

Spring MVC 


SimpleMappingExceptionResolver is an implementation of HandlerExceptionResolver. It can be used to map exception classes to view names and view names to status codes.


package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.List;
import java.util.Properties;

@EnableWebMvc
@ComponentScan
@Configuration
public class AppConfig implements WebMvcConfigurer {

@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> exceptionResolvers) {
SimpleMappingExceptionResolver s =
new SimpleMappingExceptionResolver();

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

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

//setting default error view
s.setDefaultErrorView("defaultErrorView");
//setting default status code
s.setDefaultStatusCode(400);

exceptionResolvers.add(s);
}

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExampleController {

@Autowired
private DataService dataService;

@ResponseBody
@RequestMapping("/order/{orderId}")
public String handleRequest (@PathVariable("orderId") String orderId,
Model model) {
return dataService.getOrderDetail(orderId);
}

@ResponseBody
@RequestMapping("/data/{id}")
public String testHandler (@PathVariable("id") String id,
Model model) {
return dataService.getData(id);
}

@ResponseBody
@RequestMapping("/test")
public void testHandler2 () {
throw new RuntimeException("test exception");
}
}
Original Post





@Bean
HandlerExceptionResolver customExceptionResolver () {
SimpleMappingExceptionResolver s = new SimpleMappingExceptionResolver();
Properties p = new Properties();
//mapping spring internal error NoHandlerFoundException to a view name.
p.setProperty(NoHandlerFoundException.class.getName(), "error-page");
s.setExceptionMappings(p);
//uncomment following line if we want to send code other than default 200
//s.addStatusCode("error-page", HttpStatus.NOT_FOUND.value());

//This resolver will be processed before default ones
s.setOrder(Ordered.HIGHEST_PRECEDENCE);
return s;
}
Original Post




See Also

Join