全局异常捕获处理
程序员文章站
2022-04-21 17:56:42
...
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.JpaOptimisticLockingFailureException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import com.xqx.fdczzxt.exception.ParameterCheckException;
/**
* spring mvc 全局异常捕获处理
* @author Wtao
*
*/
@Component
public class GloableExceptionHandler implements HandlerExceptionResolver {
final Logger log = LoggerFactory.getLogger(GloableExceptionHandler.class);
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
//异常日志记录级别设定
if(ex instanceof ParameterCheckException){
//业务参数异常只做warn级别日志
log.warn(ex.getMessage(),ex);
}else{
log.error(ex.getMessage(),ex);
}
//特殊异常提示
String tip = "出错了";
//乐观锁更新冲突
if(ex instanceof JpaOptimisticLockingFailureException){
tip = "更新操作冲突,请确保在同一时间只有您一人在操作该条记录。";
}
//返回页面异常页面("WEB-INF/jsp/error.jsp")
ModelAndView mv = new ModelAndView("error");
mv.addObject("exception", ex);
mv.addObject("stacktrace",ExceptionUtils.getStackTrace(ex));
mv.addObject("tip", tip);
return mv;
}
}
上一篇: EditText隐藏软键盘的小问题
下一篇: Android 隐藏软键盘功能的实现