AdaKing88
2023-08-23 ae35159387a55199e8ab150ebb97d89d68a235bd
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
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.jeecg.modules.arj.exception;
 
import org.jeecg.modules.arj.config.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * @author zk
 * @Description: 异常处理
 * @date 2021-07-14 13:58
 */
@ControllerAdvice
public class ExceptionHandle {
 
    private static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
 
 
 
    /**
     * 异常的处理
     */
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public AjaxResult handleException(Exception e){
 
        //e.printStackTrace();
        logger.error("服务器发生了异常,原因是:{}",e);
 
 
        return AjaxResult.error("数据有误");
    }
 
    /**
     * 不支持的方法
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView methodSupport(Exception e){
        ModelAndView m = new ModelAndView();
        logger.error("不正确的访问方法,原因是:{}",e.getCause());
        m.addObject("error", "不正确的访问方法");
        e.printStackTrace();
        m.setViewName("error/404");
        return m;
    }
 
}