本节讨论如何使用Spring的异常处理机制,当我们程序出现错误时,以相同的一种格式,把错误信息返回给客户端

1、创建一些自定义异常

public class TipsException extends Exception {
private static final long serialVersionUID = 2784987176856514682L; public TipsException(String string) {
super(string);
} }

2、使用@RestControllerAdvice创建一个异常处理的RESTful控制器,当其他控制器抛出异常时,会被这个控制器截获并处理

@RestControllerAdvice
public class ExceptionController { @ExceptionHandler(TipsException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public AppResult tips(TipsException e) {
return new AppResult().error(e.getMessage());
}
@ExceptionHandler()
@ResponseStatus(HttpStatus.ALREADY_REPORTED)
public AppResult spittleNotFound(Exception e) {
return new AppResult().error("内部错误!请联系系统管理员!" + e.getClass().getTypeName() + e.getMessage());
} }

这里的异常处理会返回一个和普通控制器一样的AppResult对象,保证任何时候返回给客户端的值都是统一格式的

3、运行测试

【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】04、统一处理异常的更多相关文章

  1. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成

    1.增加POM依赖 注意pagehelper插件,我重写过,可以到我的这篇文章了解https://www.cnblogs.com/LiveYourLife/p/9176934.html <dep ...

  2. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】03、创建RESTful API,并统一处理返回值

    本节应用Spring对RESTful的支持,使用了如@RestController等注解实现RESTful控制器. 如果对Spring中的RESTful不太明白,请查看相关书籍 1.创建一个数据对象, ...

  3. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】05、Shiro集成

    1.POM文件中加入Shiro和fastJSON依赖 <dependency> <groupId>org.apache.shiro</groupId> <ar ...

  4. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】01、环境准备

    开发环境 windows+STS(一个针对Spring优化的Eclipse版本)+Maven+SQLServer 环境部署 1.安装SQLServer(使用版本2008R2) 自行安装,此处略过 2. ...

  5. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目

    1.创建项目 得到项目架构 2.测试项目Web功能 默认端口为8080,运行后,输入localhost:8080/index即可访问到网页 到这里,项目构建成功!

  6. 使用 JSONDoc 记录 Spring Boot RESTful API

    这个博文可以分为两部分:第一部分我将编写一个Spring Boot RESTful API,第二部分将介绍如何使用JSONDoc来记录创建的API.做这两个部分最多需要15分钟,因为使用Spring ...

  7. spring boot RESTFul API拦截 以及Filter和interceptor 、Aspect区别

    今天学习一下RESTFul api拦截 大概有三种方式 一.通过Filter这个大家很熟悉了吧,这是java规范的一个过滤器,他会拦截请求.在springboot中一般有两种配置方式. 这种过滤器拦截 ...

  8. Spring Boot & Restful API 构建实战!

    作者:liuxiaopeng https://www.cnblogs.com/paddix/p/8215245.html 在现在的开发流程中,为了最大程度实现前后端的分离,通常后端接口只提供数据接口, ...

  9. Spring Boot - Restful API

    基本用法 @GetMapping与@PostMapping不指定参数时就是指直接使用到controller一级的url就行 @GetMapping与@PathVariable对应,前者{}中的字符串和 ...

随机推荐

  1. LOJ 2980 「THUSCH 2017」大魔法师——线段树

    题目:https://loj.ac/problem/2980 线段树维护矩阵. 然后是 30 分.似乎是被卡常了?…… #include<cstdio> #include<cstri ...

  2. SQL学习记录:定义(一)

    --1.在这里@temp是一个表变量,只有一个批处理中有效,declare @temp table; --2. 如果前面加#就是临时表,可以在tempDB中查看到,它会在最后一个使用它的用户退出后才失 ...

  3. python and 用法

    >>> 1 and [] and [1] [] >>> 1 and [2] and [1] [1] >>> 0 and [1] and [2] 0

  4. IIS的站点配置存储在applicationHost.config

    C:\Windows\System32\inetsrv\Config\applicationHost.config

  5. Linux 下 VIM 的操作

    其实VI 和vim本质上没有多大区别,,但是VIM 可以高亮关键字,使得更受青睐 vim里面有3种模式:命令模式,编辑模式,末行模式 1. vim--->:set number VIM 打开文档 ...

  6. 显示等待WebDriverWait+EC

    参考:https://www.cnblogs.com/yoyoketang/p/6538505.html 百度搜索关键字,等待搜索结果页面显示完成后,验证搜索结果的第一条记录 通过WebDriverW ...

  7. php_openssl.dll' - 找不到指定的模块。 in Unknown on line 0

    今天在windows2003配置IIS+php_openssl,php-error.log老是提示“PHPWarning:PHPStartup:Unabletoloaddynamiclibrary'C ...

  8. 10. Jmeter-后置处理器一

    jmeter-后置处理器介绍与使用一 今天我们先讲 CSS/JQuery Extractor JSON Extractor Boundary Extractor 正则表达式提取器 CSS/JQuery ...

  9. MSDN上关于WinDbg的手册

    参考:http://msdn.microsoft.com/en-us/library/windows/hardware/ff540507(v=vs.85).aspx 这是最靠谱的参考了,比.hh要直观 ...

  10. excel vlookup的使用

    表1 姓名 部门 ***   ***   ***   ***   表2 姓名 部门 ***  *** *** ***  *** ***  *** ***  找表2的姓名对应部门,粘贴在表1中 vloo ...