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异常类的更多相关文章

  1. Spring Boot 全局异常配置

    Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import o ...

  2. Spring Boot的SpringApplication类详解

    相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...

  3. Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json

    需求 在构建RestFul的今天,我们一般会限定好返回数据的格式比如: { "code": 0,  "data": {},  "msg": ...

  4. Spring Boot @ControllerAdvice+@ExceptionHandler处理controller异常

    需求: 1.spring boot 项目restful 风格统一放回json 2.不在controller写try catch代码块简洁controller层 3.对异常做统一处理,同时处理@Vali ...

  5. spring boot 拦截异常 统一处理

    spring boot 默认情况下会映射到 /error 进行异常处理,提示不友好,需要自定义异常处理,提供友好展示 1.自定义异常类(spring 对于 RuntimeException 异常才会进 ...

  6. Spring Boot 处理异常返回json

    spring boot 老版本处理异常  对于浏览器客户端,返回error数据 对于非浏览器返回json数据, 主要取决于你的请求head 是什么 但是当我们自定义了:  老版本无论请求什么都会返回j ...

  7. 【spring boot】启动类启动 错误: 找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 的解决方案

    导入的一个外部的spring boot项目,运行启动类,出现错误:找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 解决方案: 将所有错误处理完成后,再 ...

  8. Spring Boot自动配置类

    http://docs.spring.io/spring-boot/docs/current/api/overview-summary.html http://docs.spring.io/sprin ...

  9. (D)spring boot使用注解类代替xml配置实例化bean

    bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...

  10. Spring Boot 如何在类中应用配置文件

    如何在类中应用配置文件 优先级: 当前目录子目录的/config > 当前目录 > classpath的/config包 > classpath的根目录 即:越靠近的优先级越高 ** ...

随机推荐

  1. SAP云平台上的SSO Principal Propagation设置

    我今天试图使用SAP云平台的SAP WebIDE Fullstack时,发现打不开, 遇到如下错误信息: You are not authorized to work with SAP Web IDE ...

  2. 模块之 time datetime random json pickle os sys hashlib collections

    目录 1. time模块 1.1表示时间的几种方式: 1.2格式化字符串的时间格式 1.3不同格式时间的转换 2.datetim模块 3.random模块 4. json模块 4.1dumps.loa ...

  3. [nginx] nginx源码分析--proxy模式下nginx的自动重定向auto_redirect

    描述 我们配置了一个proxy模式下的nginx, upstream backend-test { server ; } server { listen ; location = /nginx/hww ...

  4. python中 "is"和"=="的区别

    python中"is"和"=="区别 在做leetcode的时候,在判断两个数据是否相等时使用了python中的is not,想着入乡随俗,既然入了python ...

  5. 基于STM8的IIC协议--实例篇--时钟模块(DS3231)读取

    1. 综述 由上篇博客可知道IIC协议如何用代码实现,本篇博客就不涉及协议内容,只讲解如何使用. 本次的实验传感为:DS3231(时钟模块),对于时钟模块的具体信息我也就不多介绍,大家可以自行度娘,具 ...

  6. 阿里巴巴开源性能监控神器Arthas jvm

    原文:https://www.cnblogs.com/testfan2019/p/11038791.html 如果问性能测试中最难的是哪部分,相信很多人会说“性能调优”.确实是这样,性能调优是一个非常 ...

  7. 智能驾驶数据后处理分析利器—INTEWORK-VDA

            随着智能驾驶技术在新车上逐步普及,车辆研发阶段需要做大量的实车测试工作,当前的测试方式主要是路采实车数据后,按标准和法规进行测试场景提取和测试数据分析.调查显示绝大部分智能驾驶研发厂商 ...

  8. java基础(7)---IO流

    一.FileWriter 导包:import java.io.FileWriter 覆盖写入: 追加写入:  写数据换行:  write方法重载: 二.编码: 三.FileReader: read重载 ...

  9. mingw控制台中文乱码

    乱码原因 直接先用一段话讲乱码原因,看不懂老老实实就往下看吧 其实我用Sublime或者VSCode等编辑器写代码出现的控制台中文乱码问题是编译器mingw输出的数据使用的是UTF-8编码,而控制台用 ...

  10. redis都有哪些数据类型?分别在哪些场景下使用比较合适?

    (1)string 这是最基本的类型了,没啥可说的,就是普通的set和get,做简单的kv缓存 (2)hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象 ...