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) { Url String = request.getRequestURL().toString(); ModelAndView mv; DefaultMsg String = " System error " ; // how to judge requests a page request or data request .page .json // requires that all project requests json data, all with the end of .json IF (url.endsWith ( " .json " ) ) { // distinguished using custom message or a system message IF (the instanceof PermissionException E) { jsonData Result = JsonData.fail (e.getMessage ()); Music Videos = new new ModelAndView ( " JSONView " , result.toMap ()); / / JSONView bean id dispatcher in the corresponding } The else{ // output unknown abnormality log.error ( " ----- ----- unknow JSON Exception URL! " + URL, E); jsonData Result = JsonData.fail (defaultMsg); Music Videos = new new ModelAndView ( " JSONView " , result.toMap ()); } } the else IF (url.endsWith ( " .page " )) { // page page using .page end 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 / the Spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd " > <! - context annotation -> <context: annotation-config /> ! <- start of the drive spring mvc functional annotation -> <MVC: annotation-driven /> ! <- pack scan start -> <context: Scan-Component Base -package = " com.mmall.controller " /> <context: Scan-Component Base -package = " com.mmall.service"/> <! - Exception class management SpringExceptionResolver -> <the bean class = " com.mmall.common.SpringExceptionResolver " /> <! - the bean @RequestMapping -> <! - a need to adapt Handler - > <the bean class = " org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping " /> <-! returns the requested process -> <the bean class = " org.springframework.web.servlet.view. BeanNameViewResolver " /> <the 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>