Spring MVC custom unified exception handling class, and output error logs on the console, mvc Exception Handling

Source: Internet
Author: User

Spring MVC custom unified exception handling class, and output error logs on the console, mvc Exception Handling

After SimpleMappingExceptionResolver is used for unified Exception Handling (refer to Spring MVC's unified exception handling method), log4j cannot output error logs in the console when exceptions occur. Therefore, you need to customize a RrtongMappingExceptionResolver class that inherits to SimpleMappingExceptionResolver. In RrtongMappingExceptionResolver, log. error (ex. getMessage () is used to output logs to the console. The specific configuration and implementation of RrtongMappingExceptionResolver are as follows.

Configure custom unified exception handling class RrtongMappingExceptionResolver
<Bean name = "exceptionResolver" class = "com. rrtong. frame. exception. RrtongMappingExceptionResolver"> <! -- Defines the variable name used to obtain the exception information on the exception handling page. The default name is exception --> <property name = "exceptionAttribute" value = "ex"> </property> <! -- Define exceptions that require special processing, and use the class name or full path name as the key, the page name is also used 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 = "errorrs/error"> 500 </ prop> <prop key = "errors/404"> 404 </prop> </props> </property> <! -- Set the log output level. If this parameter is not set, error logs such as warnings are not output by default. --> <property name = "warnLogCategory" value = "DEBUG"/> <! -- Default HTTP status code --> <property name = "defaultStatusCode" value = "500"/> </bean>
RrtongMappingExceptionResolver
/*** @ ClassName: RrtongMappingExceptionResolver * @ description: inherits from SimpleMappingExceptionResolver's custom unified Exception Handling * @ 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);/* the error log is output to the console */log. error (ex. getMessage (); return modelAndView ;}}

 

Related Article

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.