springboot:ControllerAdvice
程序员文章站
2022-06-16 08:05:04
...
全局异常拦截
//@ControllerAdvice(annotations=TestController.class)
//@ControllerAdvice(basePackages={"com.aaa","com.bbb"})
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ServiceException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
// @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
// @ExceptionHandler//处理所有异常
@ResponseBody
public APIResponse handleException(HttpServletRequest request, Exception ex) {
String message = ex.getMessage();
return APIResponse.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
}
// @ExceptionHandler(SQLException.class)
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
// @ResponseBody
// public APIResponse handleSQLException(HttpServletRequest request, Exception ex) {
// String message = ex.getMessage();
// return APIResponse.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
// }
// @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="IOException occured")
// @ExceptionHandler(IOException.class)
// @ResponseBody
// public void handleIOException(){
// //returning 404 error code
// }
//
// @ResponseStatus(HttpStatus.BAD_REQUEST)
// @ResponseBody
// @ExceptionHandler(SignException.class)
// public APIResponse signException(SignException ex) {
// return ex.getEr();
// }
}
//@ControllerAdvice(annotations=TestController.class)
//@ControllerAdvice(basePackages={"com.aaa","com.bbb"})
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ServiceException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
// @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
// @ExceptionHandler//处理所有异常
@ResponseBody
public APIResponse handleException(HttpServletRequest request, Exception ex) {
String message = ex.getMessage();
return APIResponse.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
}
// @ExceptionHandler(SQLException.class)
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
// @ResponseBody
// public APIResponse handleSQLException(HttpServletRequest request, Exception ex) {
// String message = ex.getMessage();
// return APIResponse.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
// }
// @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="IOException occured")
// @ExceptionHandler(IOException.class)
// @ResponseBody
// public void handleIOException(){
// //returning 404 error code
// }
//
// @ResponseStatus(HttpStatus.BAD_REQUEST)
// @ResponseBody
// @ExceptionHandler(SignException.class)
// public APIResponse signException(SignException ex) {
// return ex.getEr();
// }
}
上一篇: Java打印条码,使用佳博条码打印机
下一篇: springboot:热部署