29 Star 314 Fork 200

不一样的科技宅 / spring_boot_basic_project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
GlobalExceptionHandling.java 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
huangxunhui 提交于 2020-03-07 14:51 . 初次提交。
package com.hxh.basic.project.aop;
import com.hxh.basic.project.enums.ResultEnum;
import com.hxh.basic.project.exception.CustomException;
import com.hxh.basic.project.utils.ResultVoUtil;
import com.hxh.basic.project.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.util.Objects;
/**
* @author 黄训辉
* Date: Created in 2018/3/22 11:02
* Utils: Intellij Idea
* Description: 全局异常处理
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandling {
/**
* 自定义异常
*/
@ExceptionHandler(value = CustomException.class)
public ResultVo processException(CustomException e) {
log.error("位置:{} -> 错误信息:{}", e.getMethod() ,e.getLocalizedMessage());
return ResultVoUtil.error(Objects.requireNonNull(ResultEnum.getByCode(e.getCode())));
}
/**
* 拦截表单参数校验
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler({BindException.class})
public ResultVo bindException(BindException e) {
BindingResult bindingResult = e.getBindingResult();
return ResultVoUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
}
/**
* 拦截JSON参数校验
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResultVo bindException(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
return ResultVoUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
}
/**
* 参数格式错误
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResultVo methodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("错误信息{}", e.getLocalizedMessage());
return ResultVoUtil.error(ResultEnum.ARGUMENT_TYPE_MISMATCH);
}
/**
* 参数格式错误
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResultVo httpMessageNotReadable(HttpMessageNotReadableException e) {
log.error("错误信息:{}", e.getLocalizedMessage());
return ResultVoUtil.error(ResultEnum.FORMAT_ERROR);
}
/**
* 请求方式不支持
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResultVo httpReqMethodNotSupported(HttpRequestMethodNotSupportedException e) {
log.error("错误信息:{}", e.getLocalizedMessage());
return ResultVoUtil.error(ResultEnum.REQ_METHOD_NOT_SUPPORT);
}
/**
* 通用异常
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(Exception.class)
public ResultVo exception(Exception e) {
e.printStackTrace();
return ResultVoUtil.error(ResultEnum.UNKNOWN_EXCEPTION);
}
}
Java
1
https://gitee.com/huangxunhui/basic_project.git
git@gitee.com:huangxunhui/basic_project.git
huangxunhui
basic_project
spring_boot_basic_project
master

搜索帮助