Spring MVC Custom Unified exception handling class and output error log in console

Source: Internet
Author: User

After using Simplemappingexceptionresolver for uniform exception handling (refer to Spring MVC's exception-unification approach), when an exception is found, log4j cannot output the error log at the console. Therefore, you need to customize a Rrtongmappingexceptionresolver class that inherits to Simplemappingexceptionresolver, which is passed in Rrtongmappingexceptionresolver Log.error (Ex.getmessage ()) to output the log to the console. The following is the implementation of the specific configuration and rrtongmappingexceptionresolver.

Configure a custom unified exception handling class Rrtongmappingexceptionresolver
<bean name= "Exceptionresolver" class= "Com.rrtong.frame.exception.RrtongMappingExceptionResolver" > <!--definition        The exception handling page is used to get the variable name of the exception information, the default name is exception---<property name= "Exceptionattribute" value= "ex" ></property> <!--define exceptions that require special handling, with the class name or full pathname as key, and the page name as the value--><property name= "Exceptionmappings" ><props><prop Key= "Com.rrtong.frame.exception.GuideTestException"; /.. /exception/error-interface</prop><!--<prop key= "Com.rrtong.frame.exception.NotLoginException" > Login</prop>--><prop key= "Java.lang.Exception"; /..                  /exception/errorpage</prop></props></property> <property name= "StatusCodes" > <props> <prop key= "Errors/error" >500</prop> <prop key= "E rrors/404 ">404</prop> </props> </property> <!--set the log output level, do not define the default output alarm Error log information--><property name= "WarnloGcategory "value=" DEBUG "/><!--default HTTP status code--><property name=" Defaultstatuscode "value=" Bean>
Implement unified exception Handling class Rrtongmappingexceptionresolver
/**  * @className: Rrtongmappingexceptionresolver  * @description: Custom unified exception handling inherited to Simplemappingexceptionresolver * @author: Administrator * @date January 12, 2016 */public class Rrtongmappingexceptionresolver extends simplemappingexceptionresolver{private final static Logger log = Loggerfactory.getlogger (Rrtongmappingexceptionresolver.class); @Overrideprotected Modelandview doresolveexception (httpservletrequest request, HttpServletResponse Response,object Handler, Exception ex) {map<string, exception> model = new hashmap<string, exception> (); Model.put ("Ex", Ex); Modelandview Modelandview = new Modelandview (".. /.. /exception/errorpage ", model);/* error log output to console */log.error (Ex.getmessage ()); return Modelandview;}}

Spring MVC Custom Unified exception handling class and output error log in console

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.