spring-MVC和springBoot全局异常处理
2019-06-30 21:57
731 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31728205/article/details/94359275
spring-MVC
全局异常处理方法
[code]package com.exception; import com.alibaba.fastjson.support.spring.FastJsonJsonView; import com.ResponseCode; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; /** * 全局异常处理 */ public class MyExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) { ModelAndView mv = new ModelAndView(); FastJsonJsonView view = new FastJsonJsonView(); Map<String, Object> attributes = new HashMap<String, Object>(); if (ex instanceof BizException) { ResponseCode responseCode = ((BizException) ex).getResponseCode(); String exMessage = ex.getMessage(); responseCode.setDesc(exMessage); attributes.put("success", responseCode.isSuccess()); attributes.put("code", responseCode.getCode()); attributes.put("desc", responseCode.getDesc()); } else if (ex instanceof NumberFormatException) { ResponseCode responseCode = ResponseCode.OTHER_ERROR; responseCode.setDesc("数据类型有误"); attributes.put("success", responseCode.isSuccess()); attributes.put("code", responseCode.getCode()); attributes.put("desc", responseCode.getDesc()); } else { ex.printStackTrace(); ResponseCode responseCode = ResponseCode.OTHER_ERROR; attributes.put("success", responseCode.isSuccess()); attributes.put("code", responseCode.getCode()); attributes.put("desc", responseCode.getDesc()); } view.setAttributesMap(attributes); mv.setView(view); return mv; } }
异常方法
[code]package com.exception; import com.response.ResponseCode; /** * 业务异常 */ public class BizException extends RuntimeException { private static final long serialVersionUID = -4319552887844403472L; private ResponseCode responseCode; public BizException(ResponseCode responseCode, Object... args) { super(String.format(responseCode.getDesc(), args)); this.responseCode = responseCode; } public BizException() { super(); } public BizException(String message, Throwable cause) { super(message, cause); } public BizException(Throwable cause) { super(cause); } public BizException(String message) { super(message); } public ResponseCode getResponseCode() { return responseCode; } /** * 实例化异常 * * @param msgFormat * @param args * @return */ public BizException newInstance(String msgFormat, Object... args) { return new BizException(null, msgFormat, args); } }
1.spring-mvc.xml中配置
[code]<bean id="exceptionResolver" class="com.zyark.only.exception.MyExceptionHandler"/>
2.config配置
[code]import com.zyark.only.exception.MyExceptionHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.HandlerExceptionResolver; import javax.servlet.Filter; import java.util.LinkedHashMap; import java.util.Map; /** * @author zzr */ @Configuration public class DefaultShiroConfiguration { /** * 注册全局异常处理 * * @return */ @Bean(name = "exceptionHandler") public HandlerExceptionResolver handlerExceptionResolver() { return new MyExceptionHandler(); } }
springBoot
使用上面的config配置
相关文章推荐
- SpringBoot之全局异常处理
- springboot全局异常处理
- Spring Boot处理全局统一异常的两种方法与区别
- springBoot 全局异常统一处理
- Spring boot 全局异常处理
- spring boot全局异常处理
- Spring boot 七 全局异常处理
- springboot springmvc 抛出全局异常解决方法
- Spring Boot 全局异常处理 与 Hibernate Validator校验框架整合
- Spring-Boot--日志操作【全局异常捕获消息处理☞日志控制台输出+日志文件记录】
- SpringBoot入门——局部与全局的异常处理
- 第二十一章:SpringBoot项目中的全局异常处理
- SpringBoot如何优雅的处理全局异常
- SpringBoot系列之三全局异常的捕获处理
- SpringBoot全局异常处理(续)
- Spring Boot全局异常处理
- 第九篇:SpringBoot 2.x全局异常处理
- SpringBoot学习——全局异常处理设置(返回JSON)
- SpringBoot学习历程(4)->全局异常的处理
- spring boot 全局异常处理及自定义异常类