spring boot 全局异常处理方法汇总

作者:全力以赴001 时间:2021-07-06 22:44:04 

这篇文章主要介绍了spring boot 全局异常处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


import cn.sisyphe.framework.web.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
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.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

/**
* @author ming
* @desc 全局异常处理类
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

/**
  * 缺失请求参数处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(MissingServletRequestParameterException.class)
 @ResponseBody
 public ResponseResult handleMissingServletRequestParameterException(MissingServletRequestParameterException e, HttpServletRequest request) {
   String message = "缺失请求参数" + e.getParameterName();
   return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
 }

/**
  * 请求参数类型错误处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(MethodArgumentTypeMismatchException.class)
 @ResponseBody
 public ResponseResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
   String message = "请求参数" + e.getName() + "类型错误";
   return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
 }

/**
  * 参数类型错误异常类型处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(HttpMessageConversionException.class)
 @ResponseBody
 public ResponseResult handleHttpMessageNotReadableException(HttpMessageConversionException e, HttpServletRequest request) {
   String message = "参数类型错误";
   return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
 }

/**
  * 空指针异常处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(NullPointerException.class)
 @ResponseBody
 public ResponseResult handleNullPointerException(NullPointerException e, HttpServletRequest request) {
   String message = "空指针异常";
   return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e, true);
 }

/**
  * MethodArgumentNotValidException 异常处理
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(MethodArgumentNotValidException.class)
 @ResponseBody
 public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
   StringBuilder errorMsg = new StringBuilder();
   BindingResult re = e.getBindingResult();
   for (ObjectError error : re.getAllErrors()) {
     errorMsg.append(error.getDefaultMessage()).append(",");
   }
   errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
   return ackTransfer(request, errorMsg.toString(), "-1", e, false);
 }

/**
  * 绑定异常处理
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(BindException.class)
 @ResponseBody
 public ResponseResult handleBindException(BindException e,HttpServletRequest request){
   BindingResult result = e.getBindingResult();
   StringBuilder errorMsg = new StringBuilder();
   for (ObjectError error : result.getAllErrors()) {
     errorMsg.append(error.getDefaultMessage()).append(",");
   }
   errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
   return ackTransfer(request, errorMsg.toString(), "-1", e, false);
 }

/**
  * 自定义异常类型异常消息处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler({DataException.class})
 @ResponseBody
 public ResponseResult handleDataException(DataException e, HttpServletRequest request) {
   String message = e.getErrorMessage();
   String code = e.getErrorCode();
   return ackTransfer(request, code, message, e, true);
 }

/**
  * 处理运行时异常
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler({RuntimeException.class})
 @ResponseBody
 public ResponseResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
   return ackTransfer(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value() + "", e, true);
 }

/**
  * 默认异常处理
  *
  * @param e
  * @param request
  * @return
  */
 @ExceptionHandler(Exception.class)
 @ResponseBody
 public ResponseResult handleException(Exception e, HttpServletRequest request) {
   return ackTransfer(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value() + "", e, true);
 }

private ResponseResult ackTransfer(HttpServletRequest request, String message, String code, Exception e, boolean printStackTrace) {
   ResponseResult result = new ResponseResult();
   result.setCode(code);
   result.setMessage(message);
   if (printStackTrace) {
     log.error(message, e);
   } else {
     log.error(message);
   }
   return result;
 }

private ResponseResult ackTransfer(HttpServletRequest request, String message, String code, Exception e) {
   return ackTransfer(request, message, code, e, false);
 }
}

来源:https://www.cnblogs.com/huzi007/p/11684194.html

标签:spring,boot,全局,异常,处理
0
投稿

猜你喜欢

  • MyBatis 配置之集合的嵌套方式

    2023-01-15 12:26:49
  • 详解Java数据结构和算法(有序数组和二分查找)

    2023-04-08 13:38:07
  • SpringBoot搭建go-cqhttp机器人的方法实现

    2022-11-17 23:58:40
  • C# ref and out的使用小结

    2021-12-19 22:35:40
  • android操作XML的几种方法总结

    2021-08-10 06:06:16
  • MyBatis中的properties配置(推荐)

    2023-06-04 20:47:58
  • C#实现简单的JSON序列化功能代码实例

    2023-06-21 09:03:52
  • Java基于外观模式实现美食天下食谱功能实例详解

    2022-08-22 22:59:51
  • Android TextView实现带链接文字事件监听的三种常用方式示例

    2021-10-12 23:59:35
  • springboot vue完成发送接口请求显示响应头信息

    2023-06-05 02:45:34
  • Springboot实现多服务器session共享

    2022-09-06 13:58:34
  • Spring security权限配置与使用大全

    2022-03-05 15:37:21
  • 如何使用Java调用Linux系统命令

    2021-12-24 20:45:31
  • 详解Spring Cloud负载均衡重要组件Ribbon中重要类的用法

    2023-07-06 02:54:01
  • redis统计APP在线人数的实例

    2023-11-24 23:56:54
  • 详解Android自定义控件属性

    2023-09-27 18:00:34
  • MyBatis-Plus多表联查(动态查询)的项目实践

    2023-11-19 21:43:17
  • java中对字符串每个字符统计的方法

    2023-09-23 13:53:16
  • IntelliJ IDEA像Eclipse一样打开多个项目的图文教程

    2022-03-14 05:31:40
  • Android中的应用认领总结

    2022-03-12 02:54:05
  • asp之家 软件编程 m.aspxhome.com