DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Spring's SimpleMappingExceptionHandler

Using Spring's SimpleMappingExceptionHandler

Roger Hughes user avatar by
Roger Hughes
·
Feb. 21, 12 · Interview
Like (0)
Save
Tweet
Share
15.54K Views

Join the DZone community and get the full member experience.

Join For Free
My last few blogs have talked about Spring’s @ExceptionHandler annotation and how you can use it to deal with errors on a controller by controller basis to give you fine-grained control over how you handle errors in your code. The question to consider now is whether or not you always want such fine grained control, to which I’m guessing that in certain circumstances the answer will be no, and so to accommodate this Spring have provided us with the SimpleMappingExceptionHandler.

The SimpleMappingExceptionHandler is an implementation of Springs HandlerExceptionResolver class, which, as I’ve mentioned before, Spring uses to manage exceptions thrown by your code. Spring has a limitation (if that’s what it is) in that it only loads one HandlerExceptionResolver implementation at any one time. The default implementation is AnnotationMethodHandlerExceptionResolver as mentioned in my previous blogs, which forces you to make a choice between using fine-grained exception handling as provided by AnnotationMethodHandlerExceptionResolver and more coarse grain exception handling provided by SimpleMappingExceptionHandler.

The Guy’s at Spring have made adding a SimpleMappingExceptionHandler to your app fairly straight forward. To demonstrate this I first of all need a flakey controller that will throw an exception for us. The code below, lifted from my previous blog, throws an IOException when called...

  @RequestMapping(value = "/ioexception", method = RequestMethod.GET)
  public String throwAnIOException(Locale locale, Model model)
      throws IOException {

    logger.info("This will throw an IOException");

    boolean throwException = true;

    if (throwException) {
      throw new IOException("This is my IOException");
    }

    return "home";
  }

...and is guaranteed to break a webapp1

If you were intending to keep things ultra-simple, the final step would be to add the following XML to your Spring config file:

 <beans:bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  <beans:property name="exceptionMappings">
   <beans:map>
    <beans:entry key="java.io.IOException" value="io-exception" />
    <beans:entry key="java.lang.Exception" value="generic-error" />
   </beans:map>
  </beans:property>
 </beans:bean>

The XML above shows a very familiar Spring bean definition. The bean name is “exceptionResolver”, implemented by the SimpleMappingExceptionResolver class. This class has a map property, which is the most interesting part of this definition as it maps an exception class to a view name. In the sample I’m mapping IOException to a view called ‘io-exception’ and all other exceptions to a view called ‘generic-error’. Note that SimpleMappingExceptionHandler follows the standard Java exception handling rules, so when I say ‘all other exceptions’ I really mean all exceptions except IOException and its subclasses.

Although that about wraps it up for a trivial implementation. I’d like to add that I prefer to extend SimpleMappingExceptionHandler so that I can add in additional functionality, such as application specific error logging:

public class SampleExceptionHandler extends SimpleMappingExceptionResolver {

  private static final Logger logger = LoggerFactory.getLogger(SampleExceptionHandler.class);

  /**
   * Log the exception.
   *
   * @see org.springframework.web.servlet.handler.SimpleMappingExceptionResolver#doResolveException(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse, java.lang.Object,
   *      java.lang.Exception)
   */
  @Override
  protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
      Exception ex) {
    logger.error("A " + ex.getClass().getSimpleName() + " has occured in the application", ex);

    return super.doResolveException(request, response, handler, ex);
  }

}

Once you’ve written your own exception handler, then all you have to do is to modify your Spring config so that it’s picked up by Spring when the webapp loads:

<!-- Definition(s) for the SimpleMappingExceptionResolver -->
 <beans:bean id="exceptionResolver"
  class="com.captaindebug.exceptions.SampleExceptionHandler">
  <beans:property name="exceptionMappings">
   <beans:map>
    <beans:entry key="java.io.IOException" value="io-exception" />
    <beans:entry key="java.lang.Exception" value="generic-error" />
   </beans:map>
  </beans:property>
 </beans:bean>




1 The full code for this blog is available from:

git://github.com/roghughe/captaindebug.git

 

 

From http://www.captaindebug.com/2012/02/using-springs-simplemappingexceptionhan.html

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Main Pillars in ReactJS
  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • HTTP vs Messaging for Microservices Communications
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: