SpringBoot学习(三)
一、单个 controller 范围的异常处理

package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import io.swagger.annotations.Api; @Api("测试controllerAdvice和全局异常处理")
@RestController
@RequestMapping("/advice1")
public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET)
public String test1() {
throw new RuntimeException("advice1 - exception1");
} @RequestMapping(value = "/test2", method = RequestMethod.GET)
public String test2() {
throw new RuntimeException("advice1 - exception2");
} @ExceptionHandler(RuntimeException.class)
public MyExceptionResponse exceptionHandler() {
MyExceptionResponse resp = new MyExceptionResponse();
resp.setCode(300);
resp.setMsg("exception-Handler");
return resp;
} }

说明:
- 在 controller 中加入被 @ExceptionHandler 修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
- 该异常处理方法只在当前的 controller 中起作用
二、全部 controller 范围内起作用的异常处理(全局异常处理)
1、全局异常处理类

package com.xxx.secondboot.web; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse;
import com.xxx.secondboot.exception.MyRuntimeException; //@ControllerAdvice(annotations=RestController.class)
//@ControllerAdvice(basePackages={"com.xxx","com.ooo"})
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
// @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
// @ExceptionHandler//处理所有异常
@ResponseBody //在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定
public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
MyExceptionResponse resp = new MyExceptionResponse();
resp.setCode(300);
resp.setMsg("exception-Handler");
// response.setStatus(600);
return resp;
}
}

说明:
- @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类
- @ControllerAdvice 可以指定扫描范围
- @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换
- 返回 String,表示跳到某个 view
- 返回 modelAndView
- 返回 model + @ResponseBody
2、controller

package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; @Api("测试controllerAdvice和全局异常处理")
@RestController
@RequestMapping("/advice1")
public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET)
public String test1() {
throw new RuntimeException("advice1 - exception1");
} @RequestMapping(value = "/test2", method = RequestMethod.GET)
public String test2() {
throw new RuntimeException("advice1 - exception2");
} // @ExceptionHandler(RuntimeException.class)
// public MyExceptionResponse exceptionHandler() {
// MyExceptionResponse resp = new MyExceptionResponse();
// resp.setCode(300);
// resp.setMsg("exception-Handler");
// return resp;
// } }

注意:
- 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
- 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器
参考自:
- http://jinnianshilongnian.iteye.com/blog/1866350 开涛的 @ControllerAdvice(三个作用)
- http://www.tuicool.com/articles/fA7nuii springboot 约定的异常处理体系
- https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc springMVC 异常处理体系
- http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/ springMVC 异常处理体系
SpringBoot学习(三)的更多相关文章
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- springboot学习三:整合jsp
在pom.xml加入jstl <!--springboot tomcat jsp 支持开启--> <dependency> <groupId>org.apache. ...
- SpringBoot学习(三):日志
1.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息: ...
- Java开发学习(三十六)----SpringBoot三种配置文件解析
一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...
- Java开发学习(三十七)----SpringBoot多环境配置及配置文件分类
一.多环境配置 在工作中,对于开发环境.测试环境.生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql ,连接自己电脑上的 mysql 即可,但是项目开发完毕后要上线就需要该配 ...
- springboot学习(一)——helloworld
以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给 ...
- Springboot学习03-SpringMVC自动配置
Springboot学习03-SpringMVC自动配置 前言 在SpringBoot官网对于SpringMVCde 自动配置介绍 1-原文介绍如下: Spring MVC Auto-configur ...
- SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用
SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用 Spring Boot Admin是一个管理和监控Spring Boot应用程序的应用程序.本文参考文档: 官 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
随机推荐
- mysql 修改密码的几种方式
第一种方式: 最简单的方法就是借助第三方工具Navicat for MySQL来修改,方法如下: 1.登录mysql到指定库,如:登录到test库. 2.然后点击上方“用户”按钮. 3.选择要更改的用 ...
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- Laravel 5.4 migrate时报错: Specified key was too long error
Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [Illuminate\Database\QueryE ...
- h5 canvas动画,不知道谁写的
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 从ZoomEye API 到 Weblogic 弱口令扫描
参考资料: ZoomEye API: https://www.zoomeye.org/api/doc Weblogic-Weakpassword-Scnner: https://github.com/ ...
- docker 加速
Docker配置阿里云加速地址 打开阿里云网站https://cr.console.aliyun.com,登陆自己的阿里云账号. 然后只需要在服务器配置docker配置文件,只需要修改"Ex ...
- SVMtrain的参数c和g的优化
SVMtrain的参数c和g的优化 在svm训练过程中,需要对惩罚参数c和核函数的参数g进行优化,选取最好的参数 知道测试集标签的情况下 是让两个参数c和g在某一范围内取离散值,然后,取测试集分类准确 ...
- MYSQL三种安装方式--rpm包安装
1. 首先检查机器里是否已经存在MySQL $ rpm -qa | grep mysql 2. 去官网下载相应的rpm包:https://dev.mysql.com/downloads/mysql/ ...
- cenos6.5安装vsftp
1.安装 yum install vsftpd 2.配置 vi /etc/vsftpd/vsftpd.conf 检查是否如下配置 anonymous_enable=NO #禁止匿名访问 chroot_ ...
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...