GlobalExceptionHandler
老师好,请问
GlobalExceptionHandler
为什么不能捕获到filter中抛出的异常?
package com.imooc.mall.exception; import com.imooc.mall.common.ApiRestResponse; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * 描述: 处理统一异常的handler */ @ControllerAdvice public class GlobalExceptionHandler { private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(Exception.class) @ResponseBody public Object handleException(Exception e) { log.error("Default Exception: ", e); return ApiRestResponse.error(ImoocMallExceptionEnum.SYSTEM_ERROR); } @ExceptionHandler(ImoocMallException.class) @ResponseBody public Object handleImoocMallException(ImoocMallException e) { log.error("ImoocMallException: ", e); return ApiRestResponse.error(e.getCode(), e.getMessage()); } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ApiRestResponse handleMethodArgumentNotValidException( MethodArgumentNotValidException e) { log.error("MethodArgumentNotValidException: ", e); return handleBindingResult(e.getBindingResult()); } private ApiRestResponse handleBindingResult(BindingResult result) { //把异常处理为对外暴露的提示 List<String> list = new ArrayList<>(); //不初始化不能add元素 if (result.hasErrors()) { //判断是否包含错误 List<ObjectError> allErrors = result.getAllErrors(); //得到所有错误 for (ObjectError objectError : allErrors) { String message = objectError.getDefaultMessage(); //得到错误信息 list.add(message); } } if (list.size() == 0) { return ApiRestResponse.error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR); } return ApiRestResponse .error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR.getCode(), list.toString()); } }
import com.imooc.goodsshop.model.pojo.User; import com.imooc.goodsshop.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.IOException; /*if 用户未登录 ;不是管理员 return error*/ public class AdminFilter implements Filter { @Autowired private UserService userService; @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)servletRequest; HttpSession session=request.getSession(); User currentUser = (User)session.getAttribute(Constant.IMOOC_MALL_USER); if (currentUser==null) { throw new ImoocMallException(ImoocMallExceptionEnum.NOT_LOGIN); } if (userService.checkAdmin(currentUser)) { filterChain.doFilter(servletRequest, servletResponse ); }else{ throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ADMIN); } } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
13
收起
正在回答
1回答
同学你好,因为filter的执行时机在GlobalExceptionHandler生效之前,所以不能被GlobalExceptionHandler处理。
请求进入服务器后,先经过filter进行筛选,然后进入程序,此时GlobalExceptionHandler才可以生效。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星