统一异常处理:HandlerExceptionResolver
SpringExceptionResolver.java
package com.mmall.common; import com.mmall.exception.PermissionException; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*必须配置被spring管理,全局异常就会被捕捉 -> spring-servlet.xml*/ @Slf4j public class SpringExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { String url = request.getRequestURL().toString(); ModelAndView mv; String defaultMsg = "System error"; //如何判断请求时页面请求还是数据请求 .json .page //要求项目中所有请求json数据,都是用.json结尾 if (url.endsWith(".json")){ //区分使用自定义message还是系统message if (e instanceof PermissionException){ JsonData result = JsonData.fail(e.getMessage()); mv = new ModelAndView("jsonView", result.toMap());//jsonView 对应dispatcher中的bean id }else { //输出未知的异常 log.error("-----unknow json exception! url-----" + url,e); JsonData result = JsonData.fail(defaultMsg); mv = new ModelAndView("jsonView", result.toMap()); } }else if (url.endsWith(".page")){//page页面使用.page结尾 log.error("-----unknow page exception! url-----" + url,e); JsonData result = JsonData.fail(defaultMsg); mv = new ModelAndView("exception", result.toMap());//对应exception.jsp }else { log.error("-----unknow other exception! url-----" + url,e); JsonData result = JsonData.fail(defaultMsg); mv = new ModelAndView("jsonView", result.toMap());//默认也返回json格式 } return mv; } }
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--上下文注解--> <context:annotation-config /> <!--启动注解驱动的spring mvc 功能--> <mvc:annotation-driven/> <!--启动包扫描--> <context:component-scan base-package="com.mmall.controller" /> <context:component-scan base-package="com.mmall.service" /> <!--管理异常处理类 SpringExceptionResolver--> <bean class="com.mmall.common.SpringExceptionResolver"/> <!-- RequestMapping bean--><!--需要一个Handler来适配--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--处理请求的返回--> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp" /> </bean> </beans>