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)
随机推荐
- c#的WebService和调用
WebService: 1.新建一个空白web应用程序 2.在上面建立的web应用程序添加web服务 4.保存发布至 IIS Client: 1.新建一个程序(可以是winform.控制台.web) ...
- Windows server 2008 R2端口转发
查询配置了转发的端口 netsh interface portproxy show v4tov4 配置转发(所有ip访问192.168.0.99的1001端口均指向1953端口) netsh inte ...
- C#隐藏与显示系统任务栏和开始菜单栏按钮
隐藏与显示系统任务栏和开始菜单栏按钮:直接上代码: private const int SW_HIDE = 0; //隐藏 private const int SW_REST ...
- [PHP] 多进程通信-消息队列使用
向消息队列发送数据和获取数据的测试 <?php $key=ftok(__FILE__,'a'); //获取消息队列 $queue=msg_get_queue($key,0666); //发送消息 ...
- spring_07使用spring的特殊bean、完成分散配置
一. 前言 分散配置思路:创建properties文件,添加数据,在beans文件中先配置properties文件,再在bean中使用占位符引用数据 对于bean的生命周期中的很多处理接口,处 ...
- 【Java每日一题】20170315
20170314问题解析请点击今日问题下方的“[Java每日一题]20170315”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...
- 【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别
注意到这个问题,是在CentOS7上安装JDK的时候,查找相关的资料,发现安装JDK之前都需要检查或卸载系统上原生的Open JDK,这才引起了注意. 到了这里,引用查到的一篇说明. 转自:http: ...
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中的例子吧,理解起来可能更 ...
- JUnit4注解
今天学习了下,mybatis中开发dao的方法,用到了JUnit4进行单元测试, 将JUnit4中的注解总结了下,供大家参考学习: JUnit 4 开始使用 Java 5 中的注解(annotatio ...
- C#Thread的方法、Start()、Sleep(int)、Abort()、Suspend()、Resume()
Thread类有几个至关重要的方法 Start():启动线程: Sleep(int):静态方法,暂停当前线程指定的毫秒数: Abort():通常使用该方法来终止一个线程: Suspend():该方法并 ...