springboot统一异常处理类及注解参数为数组的写法
统一异常处理类
package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.common.constant.CodeConstants;
import com.wdcloud.categoryserver.common.entity.BaseView;
import org.springframework.http.converter.HttpMessageNotReadableException;
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 org.springframework.web.multipart.support.MissingServletRequestPartException; import javax.servlet.http.HttpServletRequest; /**
* @describe: 全局异常处理
* @author: zhuchunwang
* @date: 2018/5/29 17:40
* @version: 1.0
*/
@ControllerAdvice(annotations = {RestController.class})
public class GlobalExceptionHandler {
/**
* 默认未知异常
* @param req
* @param e
* @return
* @throws Exception
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public BaseView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
e.printStackTrace();
return new BaseView(CodeConstants.SYSTEM_EXCEPTION,CodeConstants.SYSTEM_EXCEPTION_MSG);
} /**
* 参数异常
* @param req
* @param e
* @return
* @throws Exception
*/
@ExceptionHandler(value = {HttpMessageNotReadableException.class, MissingServletRequestPartException.class})
@ResponseBody
public BaseView httpMessageNotReadableExceptionErrorHandler(HttpServletRequest req, Exception e) throws Exception {
e.printStackTrace();
return new BaseView(CodeConstants.PARAMETER_ERROR,CodeConstants.PARAMETER_ERROR_MSG);
}
}
一个参数时这样写
@ExceptionHandler(value = HttpMessageNotReadableException.class)
多个参数时这样写
@ExceptionHandler(value = {HttpMessageNotReadableException.class, MissingServletRequestPartException.class})
springboot统一异常处理类及注解参数为数组的写法的更多相关文章
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...
- spring 或 springboot统一异常处理
spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...
- SpringBoot 统一异常处理
统一异常处理: @ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactor ...
- SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚
通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...
- SpringBoot 全局异常处理 @RestControllerAdvice +@ExceptionHandler 请求参数校验
ControllerAdvice 指示带注释的类辅助“控制器”. 作为的特殊化@Component,允许通过类路径扫描自动检测实现类. 通常用于定义@ExceptionHandler, @InitBi ...
- springboot统一异常处理及返回数据的处理
一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...
- SpringBoot统一异常处理
/** * 异常处理器 */ @RestControllerAdvice // public class BDExceptionHandler { private Logger logger = Lo ...
- Springboot统一异常处理(@ControllerAdvice)
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...
- springboot 解决实体类值为null或者数组为空,不返回前台
一个注解解决问题 @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonInclude(JsonInclude.Include.NON_NULL)
随机推荐
- 从零开始学安全(八)●Ubuntu 16 LAMP环境搭建
1.Apache2 web 服务器的安装 : 可以先更新一下服务器 sudo apt-get update # 获取最新资源包sudo apt-get upgrade # 本机软件全部更新sudo a ...
- C- unsigned :1之位域分析
1.首先回忆结构体 我们都知道定义一个结构体可以这样的方式定义: struct Point { float x; float y; } point; //等价于: struct Point point ...
- 月薪25K的90后程序员,他们都经历了什么?
如果说薪资是检验一家公司对程序员认可的标准,那么年纪轻轻就能达到月薪 25K,一定程度上说明了公司对他创造的价值的认可. 深访10+ 名月薪25K的程序员,发现他们最常见的三种成长途径是…… 在公司发 ...
- JavaScript for/in 语句 遍历数组内容
for-in遍历 for-in是为遍历对象而设计的,不适用于遍历数组. 遍历数组的缺点:数组的下标index值是数字,for-in遍历的index值"0","1" ...
- Android 程序崩溃之后fragment出现画面重叠问题
1.解决方法: 直接在包含Fragment的Activity中复写onSaveInstanceState()方法,使其空实现 @Override protected void onSaveInstan ...
- Android为TV端助力 关于线程的那些事
今天发现之前自己一直有个误区,new Runnable(run()方法){}原来它不是一定创建一个线程 如果用主线程的handler去post(Runnable),他就不会创建子线程,而是在主线程上执 ...
- uni-app 子组件如何调用父组件的方法
1.在父组件methods中定义一个方法: changeType:function(type){ this.typeActive = type; alert(type); } 2.在父组件引用子组件时 ...
- Flutter 布局(四)- Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth详解
本文主要介绍Flutter布局中的Baseline.FractionallySizedBox.IntrinsicHeight.IntrinsicWidth四种控件,详细介绍了其布局行为以及使用场景,并 ...
- 《高性能JavaScript》--读书笔记
第一章 加载和运行 延迟脚本 defer 该属性表明脚本在执行期间不会影响到页面的构造,脚本会先下载但被延迟到整个页面都解析完毕后再运行.只适用于外部脚本 <script src="j ...
- VirtualBox网络连接方式
VirtualBox图形界面下有四种网络接入方式,它们分别是: 1.NAT 网络地址转换模式(NAT,Network Address Translation) 2.Bridged Adapter 桥接 ...