[spring-boot] ControllerAdvice异常类
package com.example.demo.exception; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap;
import java.util.Map; /**
* Anny的springboot項目異常類
*/
@ControllerAdvice
public class MyControllerAdvice { /**
* 全局捕获异常类,
* · 只作用在RequestMapping方法上,所有的异常信息都会被捕获
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Map<String,Object> errorHandler(Exception ex){
Map<String,Object> map = new HashMap<>();
map.put("code",-1);
map.put("msg",ex.getMessage());
return map;
} /**
* 自定义异常
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = BusinessException.class)
public Map<String,Object> errorBusinessHandler(BusinessException ex){
Map<String,Object> map = new HashMap<>();
map.put("code",ex.getCode());
map.put("msg",ex.getMsg());
return map;
} }
package com.example.demo.exception; /**
* 自定义异常类
*/
public class BusinessException extends RuntimeException { private int code;
private String msg; public BusinessException(int code, String msg) {
super();
this.code = code;
this.msg = msg;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
package com.example.demo.controller; import com.example.demo.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/hello")
public class HelloController { private static Logger logger = LoggerFactory.getLogger(HelloController.class); @Value("${anny.msg}")
private String msg; @RequestMapping("/springBoot")
public String Hello(){
//系统异常
// int no = 1/0;
// return msg;
//自定义异常
throw new BusinessException(999999999,"你瞅啥");
}
}
[spring-boot] ControllerAdvice异常类的更多相关文章
- Spring Boot 全局异常配置
Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import o ...
- Spring Boot的SpringApplication类详解
相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
需求 在构建RestFul的今天,我们一般会限定好返回数据的格式比如: { "code": 0, "data": {}, "msg": ...
- Spring Boot @ControllerAdvice+@ExceptionHandler处理controller异常
需求: 1.spring boot 项目restful 风格统一放回json 2.不在controller写try catch代码块简洁controller层 3.对异常做统一处理,同时处理@Vali ...
- spring boot 拦截异常 统一处理
spring boot 默认情况下会映射到 /error 进行异常处理,提示不友好,需要自定义异常处理,提供友好展示 1.自定义异常类(spring 对于 RuntimeException 异常才会进 ...
- Spring Boot 处理异常返回json
spring boot 老版本处理异常 对于浏览器客户端,返回error数据 对于非浏览器返回json数据, 主要取决于你的请求head 是什么 但是当我们自定义了: 老版本无论请求什么都会返回j ...
- 【spring boot】启动类启动 错误: 找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 的解决方案
导入的一个外部的spring boot项目,运行启动类,出现错误:找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 解决方案: 将所有错误处理完成后,再 ...
- Spring Boot自动配置类
http://docs.spring.io/spring-boot/docs/current/api/overview-summary.html http://docs.spring.io/sprin ...
- (D)spring boot使用注解类代替xml配置实例化bean
bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...
- Spring Boot 如何在类中应用配置文件
如何在类中应用配置文件 优先级: 当前目录子目录的/config > 当前目录 > classpath的/config包 > classpath的根目录 即:越靠近的优先级越高 ** ...
随机推荐
- [AIR] NativeExtension在IOS下的开发实例 --- 新建项目测试ANE(四)
来源:http://bbs.9ria.com/thread-102043-1-1.html 通过前面的努力,好了,我们终于得到了一个ANE文件了.下面我们开始新建一个Flex Mobile项目做一下测 ...
- redis 异常 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk
MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. 解决方 ...
- 微信公众号&小程序 -- 获取并解密用户数据(获取openId、unionId)
本文转自https://my.oschina.net/u/3235888/blog/832895 前言 微信小程序API文档:https://mp.weixin.qq.com/debug/wxadoc ...
- Needham-Schroeder Scyther工具形式化过程
1.Needham-Schroeder Public key Protocol 协议的通信认证的过程 顺序图的 1. A-> S : A, B 2. S->A: {Ks, ...
- IP和网络互联
IP和网络互联 IP网络互连机制: IP地址分类方法及原因: CIDR地址(无分类地址): IP分组首部格式: 数据分片方法: IP分组传输思路:
- linux上如何删除文件名乱码的文件
这里写图片描述今天在服务上发现了两个文件名是乱码的文件,如图所示.这里写图片描述于是想用rm命令把它们删掉,但提示没有此文件. 网上搜了一下,找到解决方法,首先执行ls -i命令,此时在文件前面会出现 ...
- STM32移植USB驱动总结
https://blog.csdn.net/stm32_newlearner/article/details/88095944 stm32 移植usb驱动开发 单片机 STM32单片机和51单片机 ...
- P2149 [SDOI2009]Elaxia的路线[最长公共路径]
题目描述 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia和w**每天都要奔波于宿舍和实验室之间,他们 希望在 ...
- 02_SAE中创建数据表
Step1:进入新浪云应用数据库点击应用名称,进入到该应用管理界面,在数据库服务中点击“共享型MySQL”: 开启MySQL服务,使用PHPMyAdmin管理数据库,进入MySQL数据库管理界面: S ...
- QGraphicsView,QGraphicsScene,QGraphicsItem
参考:Qt4 开发实践第八章 图形视图QGraphicsView #ifndef DRIVEDGRAPH_H #define DRIVEDGRAPH_H #include <QObject> ...