package com.dpkj.common.exception; import com.dpkj.common.vo.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * controller控制器异常处理接口实现类 *

其它module可以集成此类进行controller层的异常处理

* @author 石头人 */ @Slf4j @Component @RestControllerAdvice(annotations = {RestController.class, Controller.class}) public class ControllerAdvice { /** * controller方法中的参数校验失败,但是前提是要使用 * #@Validated注解开启参数校验 * * @param e 异常 * @return Result */ @ExceptionHandler(value = MethodArgumentNotValidException.class) public Result bindingException(MethodArgumentNotValidException e) { // 获得所有校验出错的返回集 BindingResult bindingResult = e.getBindingResult(); List fieldErrors = bindingResult.getFieldErrors(); // 循环获得所有校验异常的字段 Map fieldMap = new HashMap<>(); for (FieldError fieldError : fieldErrors) { fieldMap.put(fieldError.getField(), fieldError.getDefaultMessage()); } String errMsg = fieldMap.values().toString().replaceAll("]", "").replaceAll("\\[", ""); log.warn(errMsg); // 返回给前端 return Result.error(errMsg); } /** * 处理空指针异常 * @param nullPointerException 空指针异常 * @return Result */ @ExceptionHandler(value = NullPointerException.class) public Result nullPointException(NullPointerException nullPointerException) { log.error("空指针异常类型: {},信息: {}", nullPointerException.getClass(),nullPointerException.getMessage()); return Result.error(ErrorEnum.NULL_POINTER_EXCEPTION); } /** * 所有的运行时异常,抛出异常 * @param throwable 异常 * @return Result */ @ExceptionHandler(value = Throwable.class) public Result handleException(Throwable throwable) { log.error("异常类型: {}, {}, 信息为: {}", throwable.getCause(), throwable.getClass(), throwable.getMessage()); if (throwable instanceof RRException){ RRException rrException = (RRException) throwable; return Result.error(rrException.getCode(), rrException.getMsg()); } return Result.error(ErrorEnum.RUNTIME_EXCEPTION); } /** * http信息无可读 * @param e 异常 * @return Result */ @ExceptionHandler(value = HttpMessageNotReadableException.class) public Result httpMessageNotReadAbleException(HttpMessageNotReadableException e){ log.warn("异常类型: {} 无可读信息: {}", e.getClass(), e.getMessage()); return Result.error(ErrorEnum.HTTP_MESSAGE_NOT_READABLE_EXCEPTION); } /** * 运行时异常 * @param e 运行异常对象 * @return Result */ @ExceptionHandler(value = RuntimeException.class) public Result runtimeException(RuntimeException e){ log.error("运行时异常:{}", e.getMessage()); if (e instanceof RRException){ RRException rrException = (RRException) e; return Result.error(rrException.getCode(), rrException.getMsg()); } return Result.error(ErrorEnum.RUNTIME_EXCEPTION); } /** * 请求不支持 * @return Result */ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){ log.warn("暂不支持该请求: {}", e.getMessage()); return Result.error("暂不支持此请求方式"); } /** * 参数类型错误 * @return Result */ @ExceptionHandler(MethodArgumentTypeMismatchException.class) public Result methodArgument(MethodArgumentTypeMismatchException e){ log.warn("参数发生错误: {}", e.getMessage()); return Result.error("参数发生错误"); } /** * 缺少请求参数 * @param e 缺少请求参数异常 * @return Result */ @ExceptionHandler(MissingServletRequestParameterException.class) public Result exception(MissingServletRequestParameterException e){ log.warn("缺少请求参数: {}", e.getMessage()); return Result.error("缺少请求参数"); } }