Spring-全局异常拦截
Spring MVC那一篇里提到了异常拦截来做参数校验返回,那里是对特定的 controller 做异常捕捉,但是我们也可以选择全局拦截处理
快速开始
@ResponseBody
@ControllerAdvice
public class ExceptionAdvice {
private static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
/***
* 参数绑定异常
* @date 2018/10/16
* @param exception HttpMessageNotReadableException
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public Result<Long> messageNotReadable(HttpMessageNotReadableException exception){
InvalidFormatException formatException = (InvalidFormatException)exception.getCause();
List<JsonMappingException.Reference> e = formatException.getPath();
String fieldName = "";
for (JsonMappingException.Reference reference :e){
String fieldName = reference.getFieldName();
}
logger.error("参数不匹配"+exception);
return Result.createFailResult(fieldName+"参数类型不匹配");
}
/***
* 全局异常,如果没有匹配到上述准确的异常,都会到这里来处理
* @date 2018/10/16
* @param e 没有匹配到的全局异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
public Result<String> all(Exception e){
//这里的log使用了“,”,这样能把异常的堆栈信息全部打印出来,更容易定位bug
logger.error("异常:",e);
return Result.createFailResult("工程抢救中……请稍后再试");
}
}
@ControllerAdvice
@ControllerAdvice 默认监控所有的 @RequestMapping 方法,也可以对指定过滤的条件:
// 监控所有的被@RestController注解的Controllers类
@ControllerAdvice(annotations = RestController.class)
// 监控特定的包下的Controllers类
@ControllerAdvice("org.example.controllers")
// 监控指定类的Controllers类
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
Spring-全局异常拦截的更多相关文章
- Spring 全局异常拦截根据业务返回不同格式数据 自定义异常
1.全局异常拦截:针对所有异常进行拦截 可根据请求自定义返回格式 2.自定义异常类 处理不同业务的异常 接下来开始入手代码: 1).自定义异常类 @ControllerAdvice//添加注解 记得开 ...
- Spring 全局异常捕捉
Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...
- Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截
原文链接:https://blog.csdn.net/qq_38762313/article/details/85234594 全局异常拦截器: 解决写每个接口都需要去做容错而添加try{ ...
- SpringBoot全局异常拦截
SpringBoot全局异常捕获 使用到的技能 @RestControllerAdvice或(@ControllerAdvice+@ResponseBody) @ExceptionHandler 代码 ...
- SpringBoot 全局异常拦截捕获处理
一.全局异常处理 //Result定义全局数据返回对象 package com.xiaobing.demo001.domain; public class Result { private Integ ...
- Spring Cloud Gateway之全局异常拦截器
/** * @version 2019/8/14 * @description: 异常拦截器 * @modified: */ @Slf4j public class JsonExceptionHand ...
- @ControllerAdvice全局异常拦截
@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder ...
- springboot全局异常拦截源码解读
在springboot中我们可以通过注解@ControllerAdvice来声明一个异常拦截类,通过@ExceptionHandler获取拦截类抛出来的具体异常类,我们可以通过阅读源码并debug去解 ...
- Spring MVC中@ControllerAdvice注解实现全局异常拦截
在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...
- spring自定义controller全局异常拦截
--异常类可以按需要自定义package com.dhht.wechat.exception; import com.alibaba.fastjson.JSONObject;import org.sp ...
随机推荐
- akka开发(一)HelloWorld
package com.hfi.helloakka; import akka.actor.ActorRef; import akka.actor.Props; import akka.actor.Un ...
- OpenStack 虚机网卡的创建过程
原文链接:https://www.cnblogs.com/potato-chip/p/9127083.html OpenStack虚机网卡的创建过程 OpenStack最基本和常用的操作就是启动虚机. ...
- CRUSH map 定制实例解析
1.提取已有的CRUSH map ,使用-o参数,ceph将输出一个经过编译的CRUSH map 到您指定的文件ceph osd getcrushmap -o crushmap.txt 2.反编译你的 ...
- java的数据类型、自动拆装箱、字面量
java 中的数据类型分为两大类:值类型(基本数据类型)和引用类型(复合数据类型) 值类型分为 1,整数类型(byte,short,int,long) 2,浮点类型(float,double) 3, ...
- express 重新加载
1,res.location() 2. res.redirect() location()与redirect()的比较: Express的response对象,是对Node.js原生对象ServerR ...
- jvm(1)类的加载(三)(线程上下文加载器)
简介: 类加载器从 JDK 1.0 就出现了,最初是为了满足 Java Applet 的需要而开发出来的. Java Applet 需要从远程下载 Java 类文件到浏览器中并执行. 现在类加载器在 ...
- 【数组】Jump Game
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Postman—命令执行脚本及生成报告
前言 前面的应用中,都是在postman图形界面工具里面进行测试,但是有时候我们需要把测试脚本集成到CI平台,或者在非图形界面的系统环境下进行测试,那么我们该如果处理呢 通过newman来执行post ...
- hibernate的配置文件,使用XML方式
<?xml version="1.0" encoding="UTF-8"?> <!-- 标准的XML文件的起始行,version='1.0'表 ...
- Android_设置全屏的方法
在开发的过程中,我们有时候需要让我们应用程序全屏或者是让某个页面全屏,在今天的android小技巧中我们来讲讲如何设置我们的应用程序 全屏: 通常我们有三种方式: 1.在onCreate方法中添加代码 ...