通过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\r\n\tat net.xdclass.demo.controller.ExcpController.testExcp(ExcpController.java:11)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:634)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:748)\r\n", "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;
} }
执行结果