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());
}

最新文章

  1. Linux下Nginx负载 iis问题
  2. BFC学习笔记
  3. [转]application windows are expected to have a root view controller错误
  4. JMS【二】--ActiveMQ简单介绍以及安装
  5. IE10、IE11 无法写入Cookie
  6. oledb方式读取excel文件
  7. kgdb接收一个数据包详解
  8. WCF基金会
  9. 移动端h5页面的设计稿尺寸
  10. [HNOI2010]CHORUS 合唱队
  11. char *p[] 和char**的思考
  12. django----Form详细信息
  13. java中的exception stack有时候不输出的原因(转)
  14. L1-047. 装睡
  15. Swagger文档化restful接口
  16. Linux内核:分析coredump文件 - 内核代码崩溃
  17. linux避免crontab的执行输出将磁盘目录占满?用户的mail占用大的空间?
  18. C++/C 宏定义(define)中# ## 的含义 宏拼接
  19. Memcached理解笔记3---Memcached使用总结
  20. USACO Section 1.5 Number Triangles 解题报告

热门文章

  1. python模块hashlib & hmac
  2. [Python3] 037 函数式编程 装饰器
  3. 【转贴】GS464/GS464E
  4. JavaSE 基础
  5. Spring基于SchedulingConfigurer实现定时任务
  6. FFmpeg4.0笔记:采集桌面
  7. [Next] 六.next的优化
  8. [Next] 五.next自定义内容
  9. SQL学习(三)之子句和函数
  10. MongoDB v4.0 命令