美文网首页
springboot异常处理

springboot异常处理

作者: sunpy | 来源:发表于2022-05-06 10:57 被阅读0次

    方式1:配置方式实现


    springboot异常全局配置:applocation.yml

    server:
      port: 8771
      error:
        include-message: always
        include-exception: false
        include-stacktrace: never
    

    实验代码:

    @GetMapping("/doUser")
    @ResponseBody
    public String doUser(@RequestHeader(value = "flag") String flag) {
        if (flag.equals("1")) {
            throw new RuntimeException("发生运行异常");
        }
        return flag;
    }
    
    运行结果:

    方式2:切面代码实现


    @RestControllerAdvice
    public class CustomExtHandler {
    
        //捕获全局异常,处理所有不可知的异常
        @ExceptionHandler(value=Exception.class)
        public Object handleException(Exception e, HttpServletRequest req){
            Map<String, Object> map = new HashMap<>();
            Calendar calendar = Calendar.getInstance();
            map.put("timestamp", calendar.getTime());
            map.put("status", 500);
            map.put("error", "Internal Server Error");
            map.put("message", e.getMessage());
            map.put("path", req.getRequestURI());
            return map;
        }
    }
    
    结果:

    自定义异常处理


    public class SunpyException extends RuntimeException {
    
        private String path;
    
        private String error;
    
        private String message;
    
        private Date timestamp;
    
        private int status;
    
        public String getPath() {
            return path;
        }
    
        public void setPath(String path) {
            this.path = path;
        }
    
        public String getError() {
            return error;
        }
    
        public void setError(String error) {
            this.error = error;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public Date getTimestamp() {
            return timestamp;
        }
    
        public void setTimestamp(Date timestamp) {
            this.timestamp = timestamp;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    }
    

    实验代码:

    @GetMapping("/doUser")
    @ResponseBody
    public String doUser(@RequestHeader(value = "flag") String flag, HttpServletRequest req) throws SunpyException{
        if (flag.equals("1")) {
            SunpyException e = new SunpyException();
            e.setError("Internal Server Error");
            e.setPath(req.getRequestURI());
            e.setMessage("发生运行异常");
            e.setStatus(500);
            Calendar calendar = Calendar.getInstance();
            e.setTimestamp(calendar.getTime());
            throw e;
        }
        return flag;
    }
    

    捕获显示异常:
    可以采取上面的方式1或者方式2

    结果:

    相关文章

      网友评论

          本文标题:springboot异常处理

          本文链接:https://www.haomeiwen.com/subject/wtwayrtx.html