SimpleMappingExceptionResolver for SpringMvc declarative Exception Handling

Source: Internet
Author: User

As mentioned in the previous article, an exception blocker is configured in the spring-servlet.xml and logs are recorded. If I need to do some other processing when an exception occurs, we can customize the exception Parser:

public class MyExceptionResolver implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object obj, Exception exception) {// TODO Auto-generated method stubModelAndView result=new ModelAndView("showError");result.addObject("exception", exception);return result;}
}

Then initialize in spring-servlcet.xml:

<bean id="exceptionResolver"class="com.zah.web.controller.MyExceptionHandler"/>

However, we can no longer define the following attributes in this bean:

<property name="defaultErrorView"><value>failure</value></property><property name="exceptionMappings"><props><prop key="java.sql.SQLException">showDBError</prop><prop key="java.lang.RuntimeException">showError</prop></props></property><property name="warnLogCategory" value="WARN"></property><property name="defaultStatusCode" value="500"></property>

You need to manually process the log:

public class MyExceptionResolver implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object obj, Exception exception) {// TODO Auto-generated method stubModelAndView result=new ModelAndView("showError");result.addObject("exception", exception);return result;}private Log warnLogger;public void setWarnLogCategory(String loggerName) {this.warnLogger = LogFactory.getLog(loggerName);}protected void logException(Exception ex, HttpServletRequest request) {if (this.warnLogger != null && this.warnLogger.isWarnEnabled()) {this.warnLogger.warn("Handler execution resulted in exception", ex);}}}

The writing is a bit bad. It's a record. In fact, you can look at the simplemappingexceptionresolver source code and extend the simplemappingexceptionresolver class to meet your own business needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.