• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot全局异常处理详解

springboot 搞代码 4年前 (2022-01-05) 13次浏览 已收录 0个评论

本篇文章主要介绍了springboot全局异常处理详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、单个controller范围的异常处理

 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import io.swagger.annotations.Api; @Api("测试controllerAdvice和全局异常处理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } @ExceptionHandler(RuntimeException.class) public MyExceptionResponse exceptionHandler() { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); return resp; } } 

说明:

  1. 在controller中加入被@ExceptionHandler修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
  2. 该异常处理方法只在当前的controller中起作用

二、全部controller范围内起作用的异常处理(全局异常处理)

1、全局异常处理类

 package com.xxx.secondboot.web; import javax.servlet.http.HttpServletResponse; 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.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import com.xxx.secondboot.exception.MyRuntimeException; //@ControllerAdvice(annotations=RestController.class) //@ControllerAdvice(basePackages={"com.xxx","com.ooo"}) @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeExcepti<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>on.class) //  @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) //  @ExceptionHandler//处理所有异常 @ResponseBody //在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); //    response.setStatus(600); return resp; } } 

说明:

  1. @ControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类
  2. @ControllerAdvice可以指定扫描范围
  3. @ControllerAdvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@ResponseBody进行json转换
    1. 返回String,表示跳到某个view
    2. 返回modelAndView
    3. 返回model + @ResponseBody

2、controller

 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; @Api("测试controllerAdvice和全局异常处理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } //  @ExceptionHandler(RuntimeException.class) //  public MyExceptionResponse exceptionHandler() { //    MyExceptionResponse resp = new MyExceptionResponse(); //    resp.setCode(300); //    resp.setMsg("exception-Handler"); //    return resp; //  } } 

注意:

  1. 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
  2. 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器

以上就是springboot全局异常处理详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot全局异常处理详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址