统一异常处理类

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统一异常处理类及注解参数为数组的写法的更多相关文章

  1. Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

    在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...

  2. spring 或 springboot统一异常处理

    spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...

  3. SpringBoot 统一异常处理

    统一异常处理: @ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactor ...

  4. SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚

    通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...

  5. SpringBoot 全局异常处理 @RestControllerAdvice +@ExceptionHandler 请求参数校验

    ControllerAdvice 指示带注释的类辅助“控制器”. 作为的特殊化@Component,允许通过类路径扫描自动检测实现类. 通常用于定义@ExceptionHandler, @InitBi ...

  6. springboot统一异常处理及返回数据的处理

    一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...

  7. SpringBoot统一异常处理

    /** * 异常处理器 */ @RestControllerAdvice // public class BDExceptionHandler { private Logger logger = Lo ...

  8. Springboot统一异常处理(@ControllerAdvice)

    import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...

  9. springboot 解决实体类值为null或者数组为空,不返回前台

    一个注解解决问题 @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonInclude(JsonInclude.Include.NON_NULL)

随机推荐

  1. composer Content-Length mismatch

    今天在执行 :composer update 时一直提示: 本地 package.json如下: { "private": true, "scripts": { ...

  2. mysql使用存储过程&函数实现批量插入

    写这边文章的目的,是想结合mysql 存储过程+函数完成一个批量删除的功能吧...正好也好加深下对procedure和function的熟练操作吧...废话不多说,我就直接上表结构啦哈,如下: cre ...

  3. 26.C++- 泛型编程之类模板(详解)

    在上章25.C++- 泛型编程之函数模板(详解) 学习了后,本章继续来学习类模板   类模板介绍 和函数模板一样,将泛型思想应用于类. 编译器对类模板处理方式和函数模板相同,都是进行2次编译 类模板通 ...

  4. Reinforcement Learning: An Introduction读书笔记(3)--finite MDPs

     > 目  录 <  Agent–Environment Interface Goals and Rewards Returns and Episodes Policies and Val ...

  5. virtualbox中 清理磁盘

    1. 碎片整理 windows: 下载 sdelete 工具 执行命令: sdelete –z c:\ Linux: 执行如下命令: sudo dd if=/dev/zero of=/EMPTY bs ...

  6. 为链表数据结构实现iterator接口

    iterator作用 为所有的数据结构提供统一的访问方式. 接口对象 接口对象一共有3个方法,next()方法.return()方法.throw()方法. next() 必填 用于for..of迭代. ...

  7. linux开启新端口

    1.使用vi或者vim打开iptables配置文件: vi /etc/sysconfig/iptables 2.在编辑页面新增加信任端口: -A INPUT -m state --state NEW ...

  8. K8S 部署 ingress-nginx (三) 启用 https

    部署 https 证书 cd ~/ingress # 生成私钥 tls.key, 密钥位数是 2048 openssl genrsa -out tls.key 2048 # 使用 tls.key 生成 ...

  9. CSS 定位与Z-index

    position: static   Z-index 固定是0 position: absolute/relative/fixed   Z-index 有效 在层叠显示上,所有static定位元素看作 ...

  10. C# 获取操作系统相关的信息

    本文通过一个Demo,讲解如何通过C#获取操作系统相关的信息,如内存大小,CPU大小,机器名,环境变量等操作系统软件.硬件相关信息,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: Envir ...