springboot全局异常处理详解
一、单个controller范围的异常处理
packagecom.xxx.secondboot.web;
importorg.springframework.web.bind.annotation.ExceptionHandler;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RestController;
importcom.xxx.secondboot.exception.MyExceptionResponse;
importio.swagger.annotations.Api;
@Api("测试controllerAdvice和全局异常处理")
@RestController
@RequestMapping("/advice1")
publicclassAdviceController{
@RequestMapping(value="/test1",method=RequestMethod.GET)
publicStringtest1(){
thrownewRuntimeException("advice1-exception1");
}
@RequestMapping(value="/test2",method=RequestMethod.GET)
publicStringtest2(){
thrownewRuntimeException("advice1-exception2");
}
@ExceptionHandler(RuntimeException.class)
publicMyExceptionResponseexceptionHandler(){
MyExceptionResponseresp=newMyExceptionResponse();
resp.setCode(300);
resp.setMsg("exception-Handler");
returnresp;
}
}
说明:
- 在controller中加入被@ExceptionHandler修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
- 该异常处理方法只在当前的controller中起作用
二、全部controller范围内起作用的异常处理(全局异常处理)
1、全局异常处理类
packagecom.xxx.secondboot.web;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.web.bind.annotation.ControllerAdvice;
importorg.springframework.web.bind.annotation.ExceptionHandler;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.bind.annotation.RestController;
importcom.xxx.secondboot.exception.MyExceptionResponse;
importcom.xxx.secondboot.exception.MyRuntimeException;
//@ControllerAdvice(annotations=RestController.class)
//@ControllerAdvice(basePackages={"com.xxx","com.ooo"})
@ControllerAdvice
publicclassGlobalExceptionHandler{
@ExceptionHandler(RuntimeException.class)
//@ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
//@ExceptionHandler//处理所有异常
@ResponseBody//在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定
publicMyExceptionResponseexceptionHandler(RuntimeExceptione,HttpServletResponseresponse){
MyExceptionResponseresp=newMyExceptionResponse();
resp.setCode(300);
resp.setMsg("exception-Handler");
//response.setStatus(600);
returnresp;
}
}
说明:
- @ControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类
- @ControllerAdvice可以指定扫描范围
- @ControllerAdvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@ResponseBody进行json转换
- 返回String,表示跳到某个view
- 返回modelAndView
- 返回model+@ResponseBody
2、controller
packagecom.xxx.secondboot.web;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RestController;
importio.swagger.annotations.Api;
@Api("测试controllerAdvice和全局异常处理")
@RestController
@RequestMapping("/advice1")
publicclassAdviceController{
@RequestMapping(value="/test1",method=RequestMethod.GET)
publicStringtest1(){
thrownewRuntimeException("advice1-exception1");
}
@RequestMapping(value="/test2",method=RequestMethod.GET)
publicStringtest2(){
thrownewRuntimeException("advice1-exception2");
}
//@ExceptionHandler(RuntimeException.class)
//publicMyExceptionResponseexceptionHandler(){
//MyExceptionResponseresp=newMyExceptionResponse();
//resp.setCode(300);
//resp.setMsg("exception-Handler");
//returnresp;
//}
}
注意:
- 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
- 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。