zoukankan      html  css  js  c++  java
  • Spring Boot 知识笔记(全局异常)

    通过ControllerAdvice和ExceptionHandler捕获异常和错误信息,向前端返回json格式的状态码及异常描述信息。

    1、新建一个Controller,抛出一个异常。

    package net.xdclass.demo.controller;
    
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ExcpController {
        @GetMapping("/api/v1/test_excp")
        public Object testExcp(){
            throw new RuntimeException("exception1");
        }
    }

    2、未捕获异常的时候,访问这个接口的时候,收到的异常如下

    {
        "timestamp": "2019-06-03T11:11:41.548+0000",
        "status": 500,
        "error": "Internal Server Error",
        "message": "exception1",
        "trace": "java.lang.RuntimeException: exception1
    	at net.xdclass.demo.controller.ExcpController.testExcp(ExcpController.java:11)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.lang.Thread.run(Thread.java:748)
    ",
        "path": "/api/v1/test_excp"
    }

    3、新建一个异常处理类

    import javax.servlet.http.HttpServletRequest;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestControllerAdvice
    
    public class CustomExtHandler {
    //    private static final Logger LOG = LoggerFactory.getLogger(CustomExtHandler.class);
        @ExceptionHandler(value = Exception.class) //需要捕获异常事件的类型
        public Object handException(Exception e, HttpServletRequest request){
    //        LOG.error("url {}, msg {}",request.getRequestURL(),e.getMessage());
            Map<String,Object> map = new HashMap<>();
            map.put("code",100);
            map.put("msg",e.getMessage());
            map.put("url",request.getRequestURL());
            return map;
        }
    }

    @RestControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类

    @ExceptionHander一直监听,捕获异常后按下面的方法处理

     

    4、其他异常类型

    HttpRequestMethodNotSupportedException

    http请求的方法不正确

    MissingServletRequestParameterException

    请求参数不全

    TypeMismatchException

    请求参数类型不正确

    DataFormatException

    数据格式不正确

    UserNotFoundException

    用户没找到

    IllegalArgumentException

    非法输入

    5、自定义异常

    新建自定义异常类

    ackage net.xdclass.demo.domain;
    
    public class MyException extends Exception{
        String code;
        String msg;
        public MyException(String code,String msg){
            this.code=code;
            this.msg=msg;
        }
    
        public String getCode() {
            return code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

    新建一个controller抛出自定义异常

    package net.xdclass.demo.controller;
    
    import net.xdclass.demo.domain.MyException;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    
    @RestController
    public class ExcptionController {
        @RequestMapping("/api/v1/text_text")
        public Object index() throws MyException {
            throw new MyException("500","wrong!!!");
        }
    }

    Exceptionhandler捕捉自定义异常

    ackage net.xdclass.demo.domain;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import org.springframework.http.HttpRequest;
    import org.springframework.web.HttpRequestMethodNotSupportedException;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestControllerAdvice
    
    public class CustomExtHandler {
    //    private static final Logger LOG = LoggerFactory.getLogger(CustomExtHandler.class);
        @ExceptionHandler(value = Exception.class) //需要捕获异常事件的类型
    
    
        //捕获自定义异常事件
        @ExceptionHandler(value = MyException.class)
        public Object myException(MyException e,HttpServletRequest request){
            Map<String,Object> map = new HashMap<>();
            map.put("code",e.getCode());
            map.put("msg",e.getMsg());
            return map;
        }
    }

    运行后的结果

     

    6、返回一个自定义错误页面

    资源文件中增加myerror.html

    @RestControllerAdvice  修改为@ControllerAdvice

    package net.xdclass.demo.domain;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import org.springframework.http.HttpRequest;
    import org.springframework.web.HttpRequestMethodNotSupportedException;
    import org.springframework.web.bind.annotation.*;
    
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.HashMap;
    import java.util.Map;
    
    @ControllerAdvice
    
    public class CustomExtHandler {
    //    private static final Logger LOG = LoggerFactory.getLogger(CustomExtHandler.class);
        @ResponseBody
        @ExceptionHandler(value = Exception.class) //需要捕获异常事件的类型
        public Object handException(Exception e, HttpServletRequest request){
    //        LOG.error("url {}, msg {}",request.getRequestURL(),e.getMessage());
            Map<String,Object> map = new HashMap<>();
            map.put("code",100);
            map.put("msg",e.getMessage());
            map.put("url",request.getRequestURL());
            return map;
        }
    
        //捕获自定义异常事件,返回html页面
    
    @ExceptionHandler(value = MyException.class)
    public Object myException(MyException e,HttpServletRequest request){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("myerror.html");
    modelAndView.addObject("msg",e.getMsg());
    return modelAndView;
    } }

    执行结果

     

  • 相关阅读:
    Python 安装Twisted 提示python version 2.7 required,which was not found in the registry
    Openfire Strophe开发中文乱码问题
    css div 垂直居中
    How to create custom methods for use in spring security expression language annotations
    How to check “hasRole” in Java Code with Spring Security?
    Android 显示/隐藏 应用图标
    Android 当媒体变更后,通知其他应用重新扫描
    文件上传那些事儿
    专题:点滴Javascript
    主流动画实现方式总结
  • 原文地址:https://www.cnblogs.com/Eleven-Liu/p/10969459.html
Copyright © 2011-2022 走看看