派生自 projectDept/qhighschool

yn147
2023-11-23 bccada7cbf7eea3c37c0243d95426d1a29d9121f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.qxueyou.scc.base.handler;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * Spring MVC 异常处理类
 * @author 德虎
 *
 */
@ControllerAdvice
public class QXueyouExceptionHandler {
 
    private static Logger log = LogManager.getLogger("QXueyouExceptionHandler");
 
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e)
            throws Exception {
 
        long exepCode = System.currentTimeMillis();
        log.error(req.getRequestURL() + ",诊断代码:" + exepCode + ":" + e.getMessage(), e);
 
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
            throw e;
        }
 
        ModelAndView mav = new ModelAndView();
        mav.addObject("exepCode", String.valueOf(exepCode));
        mav.setViewName("error");
 
        return mav;
    }
 
}