首页

基于springframework的HandlerExceptionResolver接口定义处理通用公共异常逻辑处理代码示例

标签:HandlerExceptionResolver,springframework,spring-webmvc,异常拦截处理     发布时间:2018-12-11   

一、前言

基于springframeworkspring-webmvc源码包(springmvc的helloworld项目示例下载)中的org.springframework.web.servlet.HandlerExceptionResolver接口,定义通用异常逻辑处理代码示例,用于记录异常错误日志或特殊异常逻辑等场景。

二、代码示例

1. 定义异常拦截处理逻辑,并配置xml文件注册bean工厂

package com.xwood.springmvc.test.exception;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@import org.springframework.web.servlet.HandlerExceptionResolver;@b@import org.springframework.web.servlet.ModelAndView;@b@@b@public class MyExceptionHandler  implements  HandlerExceptionResolver{@b@	@b@	    @Override  @b@	    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  @b@	            Exception ex) {  @b@		    //做一些自定义方法,比如记log等  @b@		    if (ex instanceof MyException) { @b@		    	System.out.println("MyExceptionHandler  is  occured !!!");@b@		    }@b@		    @b@	        Map<String, Object> model = new HashMap<String, Object>();  @b@	        model.put("data", "");  @b@	        @b@	        return new ModelAndView("/error/jsonError", model);  @b@	   }  @b@}
<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@    xmlns:p="http://www.springframework.org/schema/p"@b@    xmlns:context="http://www.springframework.org/schema/context"@b@    xmlns:mvc="http://www.springframework.org/schema/mvc"@b@    xsi:schemaLocation="http://www.springframework.org/schema/beans@b@      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd @b@      http://www.springframework.org/schema/context@b@      http://www.springframework.org/schema/context/spring-context-4.1.xsd @b@      http://www.springframework.org/schema/mvc@b@      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">@b@ @b@   <context:component-scan base-package="com.xwood.springmvc"/>@b@    @b@   <context:annotation-config/>@b@    @b@   @b@	 <bean@b@	       class="org.springframework.web.servlet.view.InternalResourceViewResolver">@b@	       <property name="prefix">@b@	           <value>/WEB-INF/pages/</value>@b@	       </property>@b@	       <property name="suffix">@b@	           <value></value>@b@	       </property>       @b@  </bean>@b@  @b@    @b@   <bean id="FreeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">@b@          <property name="templateLoaderPath" value="/WEB-INF/pages/"/>@b@          <property name="defaultEncoding" value="UTF-8"/>@b@      </bean>@b@      <bean id="FreeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">@b@          <property name="suffix" value=".ftl"/>@b@          <property name="contentType" value="text/html;charset=UTF-8"/>  @b@      </bean>@b@      @b@      @b@      <bean id="myExceptionHandler" class="com.xwood.springmvc.test.exception.MyExceptionHandler" />@b@      @b@      @b@   @b@    @b@</beans>

2. MyException自定义异常

public class MyException extends Exception {@b@    ...@b@}

3.  HelloWorldController控制层模拟异常

import org.springframework.stereotype.Controller;@b@import org.springframework.ui.Model;@b@import org.springframework.web.bind.annotation.RequestMapping;@b@import org.springframework.web.servlet.ModelAndView;@b@@b@import com.xwood.springmvc.test.exception.MyException;@b@ @b@@Controller@b@public class HelloWorldController {@b@ @b@    @RequestMapping("/hello")@b@    public String hello(Model model) throws  Exception{@b@        model.addAttribute("greeting", "Hello Spring MVC  @ http://www.xwood.net/ ");@b@        if(true)@b@        	throw new  MyException();@b@        @b@        return "helloworld.jsp";@b@    } @b@}

5.浏览器运行http://localhost:9999/HelloSpringMVC/hello.do,日志如下

[INFO] Scanning for projects...@b@。。。@b@信息: Initializing Coyote HTTP/1.1 on http-9999@b@十二月 16, 2018 2:06:55 上午 org.apache.coyote.http11.Http11Protocol start@b@信息: Starting Coyote HTTP/1.1 on http-9999@b@@b@MyExceptionHandler  is  occured !!!@b@@b@十二月 16, 2018 2:07:06 上午 org.springframework.web.servlet.PageNotFound noHandlerFound@b@警告: No mapping found for HTTP request with URI [/HelloSpringMVC/WEB-INF/pages/error/jsonError] in DispatcherServlet with name 'spring-mvc'