SpringBoot项目中,异常拦截
2024-09-02 01:25:21
SpringBoot自带异常拦截@ControllerAdvice
1.创建一个SellerExceptionHandler类打上@ControllerAdvice标签
@ControllerAdvice
public class SellExceptionHandler {
}
2.创建异常处理程序@ExceptionHandler(value = SellerAuthorizeException.class)表示拦截的异常为SellerAuthorizeException异常
/**
* 拦截登录异常
* @return
*/
@ExceptionHandler(value = SellerAuthorizeException.class)
public ModelAndView handlerAuthorizeException(){
return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/sell/seller/toLogin");
}
3.处理异常,返回json格式内容,并且改变错误状态码
3.1发生异常
3.2处理异常
@ExceptionHandler(value = SellException.class)
@ResponseBody
public ResultVO handlerSellException(SellException e) {
return ResultVOUtil.error(e.getCode(), e.getMessage());
}
但是这里就会有一个问题,不报错了,返回状态码为200,即正确
3.3@ResponseStatus(HttpStatus.FORBIDDEN),设定返回状态码
@ExceptionHandler(value = SellException.class)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResultVO handlerSellException(SellException e) {
return ResultVOUtil.error(e.getCode(), e.getMessage());
}
最新文章
- Linux下Nginx负载 iis问题
- BFC学习笔记
- [转]application windows are expected to have a root view controller错误
- JMS【二】--ActiveMQ简单介绍以及安装
- IE10、IE11 无法写入Cookie
- oledb方式读取excel文件
- kgdb接收一个数据包详解
- WCF基金会
- 移动端h5页面的设计稿尺寸
- [HNOI2010]CHORUS 合唱队
- char *p[] 和char**的思考
- django----Form详细信息
- java中的exception stack有时候不输出的原因(转)
- L1-047. 装睡
- Swagger文档化restful接口
- Linux内核:分析coredump文件 - 内核代码崩溃
- linux避免crontab的执行输出将磁盘目录占满?用户的mail占用大的空间?
- C++/C 宏定义(define)中# ## 的含义 宏拼接
- Memcached理解笔记3---Memcached使用总结
- USACO Section 1.5 Number Triangles 解题报告