摘要:一使用实现全局异常处理注解定义全局异常处理类指定自定义错误处理方法拦截的异常类型同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器定义异常业务类异常年月日定义自定义异常无数据系统异常年月日定义全局异常处理类异常
一、springboot Restful使用@ControllerAdvice、@ExceptionHandler、@ResponseBody实现全局异常处理
@ControllerAdvice 注解定义全局异常处理类
@ExceptionHandler 指定自定义错误处理方法拦截的异常类型
同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器
1.定义异常业务类
</>复制代码
/**
* 异常VO
*
* @date 2017年2月17日
* @since 1.0.0
*/
public class ExceptionVO {
private String errorCode;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
2.定义自定义异常
</>复制代码
package exception;
/**
* 无数据Exception
*
* @date 17/4/25
* @since 1.0.0
*/
public class NotFoundException extends SystemException {
public NotFoundException(String message) {
super(message);
}
}
/**
* 系统异常
*
* @date 2017年2月12日
* @since 1.0.0
*/
public class SystemException extends RuntimeException {
private static final long serialVersionUID = 1095242212086237834L;
protected Object errorCode;
protected Object[] args;
public SystemException() {
super();
}
public SystemException(String message, Throwable cause) {
super(message, cause);
}
public SystemException(String message) {
super(message);
}
public SystemException(String message, Object[] args, Throwable cause) {
super(message, cause);
this.args = args;
}
public SystemException(String message, Object[] args) {
super(message);
this.args = args;
}
public SystemException(Object errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}
public SystemException(Object errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public SystemException(Object errorCode, String message, Object[] args, Throwable cause) {
super(message, cause);
this.args = args;
this.errorCode = errorCode;
}
public SystemException(Object errorCode, String message, Object[] args) {
super(message);
this.args = args;
this.errorCode = errorCode;
}
public SystemException(Throwable cause) {
super(cause);
}
public Object[] getArgs() {
return args;
}
public Object getErrorCode() {
return errorCode;
}
}
3.定义全局异常处理类
</>复制代码
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import NotFoundException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
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.ResponseStatus;
import ExceptionVO;
/**
* WEB异常处理器
*
* @date 2017年2月16日
* @since 1.0.0
*/
@ControllerAdvice("web") //指定异常处理期拦截范围
public class WebExceptionHandler {
static Logger LOG = LoggerFactory.getLogger(WebExceptionHandler.class);
@Autowired
private MessageSource messageSource;
@ExceptionHandler(FieldException.class)
@ResponseStatus(HttpStatus.CONFLICT) //指定http响应状态
@ResponseBody
/**
* 未找到数据
*
* @param e
* @return
*/
@ExceptionHandler(NotFoundException.class)//指定异常类型
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public ExceptionVO handleNotFoundException(NotFoundException e) {
ExceptionVO vo = new ExceptionVO();
fillExceptionVO(e, vo);
return vo;
}
@ExceptionHandler(SystemException.class)
@ResponseStatus(HttpStatus.CONFLICT)
@ResponseBody
public ExceptionVO handleSystemException(SystemException e) {
ExceptionVO vo = new ExceptionVO();
fillExceptionVO(e, vo);
return vo;
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public void globalError(Exception e) {
LOG.error(e.getMessage(), e);
}
/**
* 填充异常响应消息
*
* @param e
* @param vo
*/
private void fillExceptionVO(SystemException e, ExceptionVO vo) {
if (e.getMessage() != null) {
String message = e.getMessage();
try {
message = messageSource.getMessage(e.getMessage(), e.getArgs(), LocaleContextHolder.getLocale());
} catch (NoSuchMessageException ex) {
; // ignore
}
vo.setMessage(message);
}
vo.setErrorCode(String.valueOf(e.getErrorCode()));
}
}
二、springboot 返回 ModelAndView
</>复制代码
package exception.handler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
@Commpent
public class OverallExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
Exception ex) {
ModelAndView mav = new ModelAndView();
System.out.println(ex.getMessage());
mav.addObject("errMsg", ex.getMessage());
mav.setViewName("error");
return mav;
}
}
其它方式:
</>复制代码
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ModelAndView resolveException(HttpServletRequest request, Exception ex) throws Exception {
ModelAndView mav = new ModelAndView();
System.out.println(ex.getMessage());
mav.addObject("errMsg", ex.getMessage());
mav.setViewName("error");
return mav;
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/69000.html
摘要:因为抽象类天生就是用来被继承的。由于不支多继承,子类不能够继承多个类,但可以实现多个接口如果基本功能在不断改变,那么就需要使用抽象类。全局异常处理接下来,我们在看看控制统一的异常拦截机制。 3、Spring Boot 缓存配置、全局异常处理 说明 如果您有幸能看到,请认阅读以下内容; 1、本项目临摹自abel533的Guns,他的项目 fork 自 stylefeng 的 Guns!...
摘要:和的区别方法注解作用于级别注解为一个定义一个异常处理器类注解作用于整个工程注解定义了一个全局的异常处理器需要注意的是的优先级比高即抛出的异常如果既可以让标注的方法处理又可以让标注的类中的方法处理则优先让标注的方法处理处理中的异常为了方便地展 @ControllerAdvice 和 @ExceptionHandler 的区别 ExceptionHandler, 方法注解, 作用于 Co...
摘要:前言如题,今天介绍是如何统一处理全局异常的。主要是用于异常拦截出获取并将设置到消息类中返回。状态码异常拦截类通过加入来声明该类可拦截请求,同时在方法加入并在该注解中指定要拦截的异常类。测试访问测试正常返回数据结果。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 SpringBoot 是如何统一处理全局异常的。SpringBoot 中...
摘要:在学校做一个校企合作项目,注册登录这一块需要对注册登录进行输入合法的服务器端验证,因为是前后端分离开发,所以要求返回数据。 在学校做一个校企合作项目,注册登录这一块需要对注册登录进行输入合法的服务器端验证,因为是前后端分离开发,所以要求返回JSON数据。方法有很多,这觉得用全局异常处理比较容易上手 全局异常处理 首先来创建一个sprIngboot的web项目或模块,目录结构如下 sho...
摘要:为了贴合主题,本次主要针对全局异常处理进行举例说明。自定义异常处理自定义一个异常自定义异常程序员小明错误码错误信息显而易见,这个异常继承了,属于运行时异常。包括处理其他异常,都是这种方式。 之前用springboot的时候,只知道捕获异常使用try{}catch,一个接口一个try{}catch,这也是大多数开发人员异常处理的常用方式,虽然屡试不爽,但会造成一个问题,就是一个Contr...
阅读 2964·2021-11-11 10:58
阅读 2031·2021-10-11 10:59
阅读 3572·2019-08-29 16:23
阅读 2444·2019-08-29 11:11
阅读 2857·2019-08-28 17:59
阅读 3949·2019-08-27 10:56
阅读 2190·2019-08-23 18:37
阅读 3182·2019-08-23 16:53